Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
308 views
in Technique[技术] by (71.8m points)

arrays - 从文本视图数组使用func辞职第一响应者(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.

(我的代码使用didTapAdd函数向视图控制器添加了无数的textviews。)

The problem is I cant move it because it is a textview.

(问题是我无法移动它,因为它是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.

(我想以某种方式做类似ont off的开关,每当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)
}
}
  ask by max Smtih translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

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

var arrTextFields = [UITextView]()

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

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

arrTextFields.append(subview)

Now inside didTapAdd2 method, write following code

(现在在didTapAdd2方法中,编写以下代码)

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

I hope this will fix your issue.

(希望这能解决您的问题。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...