使用文本视图数组中的func辞职第一响应者

问题描述 投票:0回答:1

我的代码使用didTapAdd函数向视图控制器添加了无数的textview。问题是我无法移动它,因为它是textview。如果它是标签或图像视图,这将完美地工作。我想以某种方式做类似ont off switch Everttime didtapadd2的事情,我希望所有textview都不能键入,以便我可以移动它们,然后再次调用func将其关闭。

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




}

@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)
}
}
arrays swift switch-statement resignfirstresponder isuserinteractionenabled
1个回答
0
投票

我为您的代码提供解决方案,希望这是您想要的。

因此,首先在UITextView变量下面创建ht的数组,如下所示:

var arrTextFields = [UITextView]()

然后在didTapAdd方法内部在数组内添加子视图,因此在ht += 50之后添加以下行

arrTextFields.append(subview)

现在在didTapAdd2方法内部,编写以下代码

@objc func didTapAdd2() {

    cutOff.isSelected = !cutOff.isSelected

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

我希望这能解决您的问题。

© www.soinside.com 2019 - 2024. All rights reserved.