【问题标题】:using a func resign first responder from a array of textviews使用来自 textviews 数组的 func resign 第一响应者
【发布时间】:2019-11-30 18:31:49
【问题描述】:

我的代码使用 didTapAdd 函数向视图控制器添加无穷无尽的文本视图。问题是我不能移动它,因为它是一个文本视图。如果它是标签或图像视图,这将完美地工作。不知何故,我想做一个类似于 on-off 开关 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)
}
}

【问题讨论】:

  • 您可以像这样禁用 textView:textView.isEnabled = false。在你的 handlePanGesture 函数的开头添加它。

标签: arrays swift switch-statement resignfirstresponder isuserinteractionenabled


【解决方案1】:

我有你的代码的解决方案,我希望这是你正在寻找的。​​p>

所以,首先在ht 变量下方创建UITextView 数组,如下所示:

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

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

【讨论】:

  • 用您的代码编辑了我的代码。似乎没有任何效果。问题已用您上面的代码更新。那是我使用的确切代码。
  • 您错过了线路。 arrTextFields.append(subview)
  • @maxSmtih,你能通过上面的代码移动文本视图吗?
  • 调用 didtapadd2 后无法全部移动,并且仍然选择了一个文本字段。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-06
  • 1970-01-01
  • 2011-06-21
  • 1970-01-01
相关资源
最近更新 更多