Ios 向标签添加手势?

Ios 向标签添加手势?,ios,swift,uilabel,uigesturerecognizer,unrecognized-selector,Ios,Swift,Uilabel,Uigesturerecognizer,Unrecognized Selector,Swift 4 iOS 11 尝试使用此代码向标签添加手势时,它会触发,但会使用无法识别的选择器崩溃 尝试使用@objc和不使用@objc,也尝试使用@selector(ViewController.copyURL),尝试打开isUserInteractiveEnabled=true;无效 @IBOutlet weak var zeroURL: UILabel! let press = UILongPressGestureRecognizer(target: zeroURL, action:

Swift 4 iOS 11

尝试使用此代码向标签添加手势时,它会触发,但会使用无法识别的选择器崩溃

尝试使用@objc和不使用@objc,也尝试使用@selector(ViewController.copyURL),尝试打开isUserInteractiveEnabled=true;无效

@IBOutlet weak var zeroURL: UILabel!

let press = UILongPressGestureRecognizer(target: zeroURL, action: #selector(copyURL))
view.addGestureRecognizer(press)

@objc func copyURL() {
    UIPasteboard.general.string = self.zeroURL.text
    print("copied")
    zeroURL.alpha = 0
    UIView.animate(withDuration: 0.75, delay: 0.25, options: [.curveEaseOut], animations: {
        self.zeroURL.alpha = 1.0
    }) { (status) in
        // do nothing
    }
}
替换这个

let press = UILongPressGestureRecognizer(target: zeroURL, action: #selector(copyURL))
view.addGestureRecognizer(press)

因为目标应该包含选择器方法的实现

替换此

let press = UILongPressGestureRecognizer(target: zeroURL, action: #selector(copyURL))
view.addGestureRecognizer(press)


由于目标应该包含选择器方法的实现

目标应该是self,因此需要将手势识别器添加到标签,而不是视图

let press = UILongPressGestureRecognizer(target: self, action: #selector(copyURL))
zeroURL.addGestureRecognizer(press)

在这里的第一行,您正在配置手势识别器,告诉它识别手势时要使用的目标操作是
self.copyURL
。第二行将手势添加到
UILabel

目标应该是self,您需要将手势识别器添加到标签,而不是视图

let press = UILongPressGestureRecognizer(target: self, action: #selector(copyURL))
zeroURL.addGestureRecognizer(press)

在这里的第一行,您正在配置手势识别器,告诉它识别手势时要使用的目标操作是
self.copyURL
。第二行将手势添加到
UILabel

使目标
self
而不是
zeroURL
,如下所示:

let press = UILongPressGestureRecognizer(target: self, action:#selector(copyURL))
zeroURL.addGestureRecognizer(press)

@objc func copyURL() {
    UIPasteboard.general.string = self.zeroURL.text
    print("copied")
    zeroURL.alpha = 0
    UIView.animate(withDuration: 0.75, delay: 0.25, options: [.curveEaseOut], animations: {
    self.zeroURL.alpha = 1.0
    }) { (status) in
        // do nothing
    }
}
zeroURL.addGestureRecognizer(press)
编辑:仅在
ui标签上添加长按手势
,而不是在
视图上添加长按手势
,如下所示:

let press = UILongPressGestureRecognizer(target: self, action:#selector(copyURL))
zeroURL.addGestureRecognizer(press)

@objc func copyURL() {
    UIPasteboard.general.string = self.zeroURL.text
    print("copied")
    zeroURL.alpha = 0
    UIView.animate(withDuration: 0.75, delay: 0.25, options: [.curveEaseOut], animations: {
    self.zeroURL.alpha = 1.0
    }) { (status) in
        // do nothing
    }
}
zeroURL.addGestureRecognizer(press)

使目标
self
而不是
zeroURL
如下所示:

let press = UILongPressGestureRecognizer(target: self, action:#selector(copyURL))
zeroURL.addGestureRecognizer(press)

@objc func copyURL() {
    UIPasteboard.general.string = self.zeroURL.text
    print("copied")
    zeroURL.alpha = 0
    UIView.animate(withDuration: 0.75, delay: 0.25, options: [.curveEaseOut], animations: {
    self.zeroURL.alpha = 1.0
    }) { (status) in
        // do nothing
    }
}
zeroURL.addGestureRecognizer(press)
编辑:仅在
ui标签上添加长按手势
,而不是在
视图上添加长按手势
,如下所示:

let press = UILongPressGestureRecognizer(target: self, action:#selector(copyURL))
zeroURL.addGestureRecognizer(press)

@objc func copyURL() {
    UIPasteboard.general.string = self.zeroURL.text
    print("copied")
    zeroURL.alpha = 0
    UIView.animate(withDuration: 0.75, delay: 0.25, options: [.curveEaseOut], animations: {
    self.zeroURL.alpha = 1.0
    }) { (status) in
        // do nothing
    }
}
zeroURL.addGestureRecognizer(press)

什么是您的
zeroURL
?什么是您的
zeroURL
?是的,这就解决了它。。。但我现在到处都有记者。我只想在zeroURL上长按一下。是的,这就解决了它。。。但我现在到处都有记者。我只想长按zeroURL。