arraysswiftswitch-statementresignfirstresponderisuserinteractionenabled

using a func resign first responder from a array of textviews


My code uses the didTapAdd func to add a endless amount of textviews to a view controller. The problem is I cant move it because it is a textview. This would work perfectly if it was a label or imageview. Somehow I want to do something like a on off switch everttime didtapadd2 is called i want all of the textviews to not be able to type into so i can move them around and then the func is call again to turn it off.

import UIKit
class ViewController: UIViewController {
var addButton = UIButton()
var cutOff = UIButton()
var count: Int = 0
var ht = -90

var arrTextFields = [UITextView]()
override func viewDidLoad() {
    super.viewDidLoad()
    view.addSubview(addButton);view.addSubview(cutOff)
    cutOff.backgroundColor = .systemPink
    addButton.backgroundColor = .systemOrange
    addButton.frame = CGRect(x: view.bounds.midX - 80, y: view.bounds.midY, width: 50, height: 50)
    cutOff.frame = CGRect(x: view.bounds.midX - 80, y: view.bounds.midY+60, width: 50, height: 50)
    addButton.addTarget(self, action: #selector(didTapAdd), for: .touchUpInside)
    cutOff.addTarget(self, action: #selector(didTapAdd2), for: .touchUpInside)
}

@objc func didTapAdd() {


    let subview = UITextView()

    subview.isUserInteractionEnabled = true
    subview.isMultipleTouchEnabled = true
    arrTextFields.append(subview)
    view.addSubview(subview)
    subview.frame = CGRect(x: view.bounds.midX - 0, y: view.bounds.midY + CGFloat(ht), width: 50, height: 30)
    subview.backgroundColor = .systemTeal
    subview.tag = count

    let pan = UIPanGestureRecognizer(target: self, action: #selector(handlePanGesture(_:)))
    subview.addGestureRecognizer(pan)

    count += 1
    ht += 50

    arrTextFields.append(subview)


}

@objc func didTapAdd2() {


    cutOff.isSelected = !cutOff.isSelected

    arrTextFields.forEach { $0.isScrollEnabled = cutOff.isSelected }


}
@objc func handlePanGesture(_ gesture: UIPanGestureRecognizer) {
    let draggedView = gesture.view!
    view.bringSubviewToFront(draggedView)
    let translation = gesture.translation(in: view)
    draggedView.center = CGPoint(x: draggedView.center.x + translation.x, y: draggedView.center.y + translation.y)
    gesture.setTranslation(.zero, in: view)
}
}

Solution

  • I have solution for your code, I hope this is what you are looking for.

    So, first create array of UITextView just below ht variable as follow:

    var arrTextFields = [UITextView]()
    

    Then inside didTapAdd method add subview inside array, so add following line after ht += 50

    arrTextFields.append(subview)
    

    Now inside didTapAdd2 method, write following code

    @objc func didTapAdd2() {
        self.view.endEditing(true)
        cutOff.isSelected = !cutOff.isSelected
        arrTextFields.forEach { $0.isScrollEnabled = cutOff.isSelected }
    }
    

    I hope this will fix your issue.