gpt4 book ai didi

ios - iOS swift 拖动2个元素

转载 作者:行者123 更新时间:2023-11-30 14:02:03 24 4
gpt4 key购买 nike

请在下面找到我在同一 UI 中拖动 2 个元素的代码,但似乎不适用于 2 个元素,当我从一个元素开始时一切正常。

当我添加第二个元素时,我在应用程序委托(delegate)中遇到此错误:

libc++abi.dylib: terminating with uncaught exception of type NSException

你们有更好的练习吗?

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

let label = UILabel(frame:CGRectMake(self.view.bounds.width / 4 - 100, self.view.bounds.height / 2 - 50, 200, 100))
let label2 = UILabel(frame:CGRectMake(self.view.bounds.width / 1 - 200, self.view.bounds.height / 2 - 50, 200, 100))
label.text = "Drag me!"
label.textAlignment = NSTextAlignment.Center
self.view.addSubview(label)

label2.text = "drag me2!"
label2.textAlignment = NSTextAlignment.Center
self.view.addSubview(label2)

let gesture = UIPanGestureRecognizer(target: self, action: Selector("wasDragged"))
label.addGestureRecognizer(gesture)

label.userInteractionEnabled = true


let gesture2 = UIPanGestureRecognizer(target: self, action: ("wasDragged2"))
label2.addGestureRecognizer(gesture2)
label2.userInteractionEnabled = true


}

func wasDragged(gesture: UIPanGestureRecognizer) {

print("was dragged")

}

func wasDragged2(gesture2: UIPanGestureRecognizer) {

print("was draged2")



}


override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

}

最佳答案

您的操作选择器“wasDragged”“wasDragged2”是错误的。这些不是您的函数的名称。函数的名称为 "wasDragged:""wasDragged2:"(请注意每个字符串名称末尾的冒号)。

关于ios - iOS swift 拖动2个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32878443/

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