我试图在按钮按下时添加UILabels和其他UIView元素,并让它们充当独立元素,但是我没有运气。我可以成功添加多个标签和文本字段,但是当我尝试使用手势识别器删除它们时,它只会删除最新的UIView元素。我的完整代码发布在下面。我的实现中存在两个主要错误:一次创建多个标签或文本字段会导致手势无响应,并且我只能删除最新的UIView元素。任何帮助是极大的赞赏!

我的模特:

import Foundation
import UIKit

class QuizItem: UIViewController{

var alert = UIAlertController()
var labelCountStepper = 0
var tfCountStepper = 0
let gestureRecog = myLongPress(target: self, action: #selector(gestureRecognized(sender:)))
let moveGesture = myPanGesture(target: self, action: #selector(userDragged(gesture:)))

func createLabel(){
    var randLabel = UILabel(frame: CGRect(x: 200, y: 200, width: 300, height: 20))
    randLabel.isUserInteractionEnabled = true
    randLabel.textColor = UIColor .black
    randLabel.text = "I am a Test Label"
    randLabel.tag = labelCountStepper
    gestureRecog.quizItem = randLabel
    moveGesture.quizItem = randLabel
    randLabel.addGestureRecognizer(self.longPressGesture())
    randLabel.addGestureRecognizer(self.movePanGesture())
    topViewController()?.view.addSubview(randLabel)
    labelCountStepper = labelCountStepper+1
}

func createTextField(){
    var randTextField = UITextField(frame: CGRect(x: 200, y: 200, width: 300, height: 35))
    randTextField.isUserInteractionEnabled = true
    randTextField.backgroundColor = UIColor .lightGray
    randTextField.placeholder = "Enter your message..."
    randTextField.tag = tfCountStepper
    gestureRecog.quizItem = randTextField
    moveGesture.quizItem = randTextField
    randTextField.addGestureRecognizer(self.longPressGesture())
    randTextField.addGestureRecognizer(self.movePanGesture())
    topViewController()?.view.addSubview(randTextField)
    tfCountStepper = tfCountStepper+1
}



func longPressGesture() -> UILongPressGestureRecognizer {
    let lpg = UILongPressGestureRecognizer(target: self, action: #selector(gestureRecognized(sender:)))
    lpg.minimumPressDuration = 0.5
    return lpg
}

func movePanGesture() -> UIPanGestureRecognizer {
    let mpg = UIPanGestureRecognizer(target: self, action: #selector(userDragged(gesture:)))
    return mpg
}

@objc func gestureRecognized(sender: UILongPressGestureRecognizer){
    if(sender.state == .began){
        print("Label Tag #: \(labelCountStepper)")
        print("Text Field Tag #: \(tfCountStepper)")
        alert = UIAlertController(title: "Remove Item?", message: nil, preferredStyle: .alert)

        alert.addAction(UIAlertAction(title: "Yes", style: .default, handler: { (action) -> Void in
            self.gestureRecog.quizItem.removeFromSuperview()
        }))

        alert.addAction(UIAlertAction(title: "No", style: .cancel){(_) in

        })

        topViewController()?.present(alert, animated: true, completion: nil)
    }
}

@objc func userDragged(gesture: UIPanGestureRecognizer){
    let loc = gesture.location(in: self.view)
    moveGesture.quizItem.center = loc

}

override func viewDidLoad() {
    super.viewDidLoad()
}

func topViewController() -> UIViewController? {
    guard var topViewController = UIApplication.shared.keyWindow?.rootViewController else { return nil }
    while topViewController.presentedViewController != nil {
        topViewController = topViewController.presentedViewController!
    }
    return topViewController
}

}

我的ViewController:
import UIKit

class ViewController: UIViewController {

var labelMaker = QuizItem()


@IBAction func createLabel(_ sender: UIButton) {
    labelMaker.createLabel()
}

@IBAction func createTextField(_ sender: UIButton) {
    labelMaker.createTextField()
}
override func viewDidLoad() {

}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

}

我还做了两个继承自UILongPress和UIPan Gesture Recognizers的子类。我将只发布LongPress,因为UIPan完全相同-只是继承自UIPan而不是UILongPress。
import Foundation
import UIKit
class myLongPress: UILongPressGestureRecognizer{

var quizItem = UIView()

}

最佳答案

您可以通过如下更改代码来实现:

@objc func gestureRecognized(sender: UILongPressGestureRecognizer){
    if(sender.state == .began){
        print("Label Tag #: \(labelCountStepper)")
        print("Text Field Tag #: \(tfCountStepper)")
        alert = UIAlertController(title: "Remove Item?", message: nil, preferredStyle: .alert)

        alert.addAction(UIAlertAction(title: "Yes", style: .default, handler: { (action) -> Void in
            let selectedView = sender.view
            selectedView?.removeFromSuperview()
        }))

        alert.addAction(UIAlertAction(title: "No", style: .cancel){(_) in

        })

        topViewController()?.present(alert, animated: true, completion: nil)
    }
}

@objc func userDragged(gesture: UIPanGestureRecognizer){
    let loc = gesture.location(in: self.view)
    let selectedView = gesture.view
    selectedView?.center = loc
}

07-24 09:44
查看更多