arrays - 使用来自 textviews 数组的 func resign 第一响应者
问题描述
我的代码使用 didTapAdd 函数向视图控制器添加无穷无尽的文本视图。问题是我不能移动它,因为它是一个文本视图。如果它是标签或图像视图,这将完美地工作。不知何故,我想做一个类似于开关 everttime didtapadd2 的事情,我希望所有的文本视图都不能输入,所以我可以移动它们,然后再次调用 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
方法内部添加子视图在数组中,所以在后面添加以下行ht += 50
arrTextFields.append(subview)
现在在didTapAdd2
方法内部,编写以下代码
@objc func didTapAdd2() {
self.view.endEditing(true)
cutOff.isSelected = !cutOff.isSelected
arrTextFields.forEach { $0.isScrollEnabled = cutOff.isSelected }
}
我希望这能解决您的问题。
推荐阅读
- c++ - 从远程客户端获取 IP 地址:remote_endpoint:传输端点未连接
- javascript - Handlebars.js:{{ this.url }} 无法在其中工作
- html - 两行导航,第二行元素等宽导航对齐
- sql-server - 如何为每次执行将过程结果存储在 SQL Server 中的新(单个)文本文件中
- mysql - MySQL:使用 IN (@variable) 更新表失败
- arrays - Mongo 错误 - 无法规范化查询:BadValue 不支持的投影选项
- java - Docker多阶段maven构建拆分依赖解析和编译
- hybris - 如何重命名现有的 Hybris 扩展
- nearley - 为什么我的 Nearley 语法会导致循环?
- git - 如何使用 git / jenkins C# 生成增量构建