gpt4 book ai didi

arrays - 使用函数从 TextView 数组中退出第一响应者

转载 作者:行者123 更新时间:2023-11-30 10:31:28 25 4
gpt4 key购买 nike

我的代码使用 didTapAdd 函数将无数的 TextView 添加到 View Controller 。问题是我无法移动它,因为它是 TextView 。如果它是标签或 ImageView ,这将非常有效。不知何故,我想做一些类似打开关闭开关的事情,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

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)
}
}

最佳答案

我有适合您的代码的解决方案,希望这就是您正在寻找的。

因此,首先创建 UITextView 数组就在ht下方变量如下:

var arrTextFields = [UITextView]()

然后里面didTapAdd方法在数组内添加 subview ,因此在 ht += 50 之后添加以下行

arrTextFields.append(subview)

现在里面didTapAdd2方法,编写如下代码

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

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

关于arrays - 使用函数从 TextView 数组中退出第一响应者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59119075/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com