[英]Type 'UIGestureRecognizer' has no member 'State'
I get the following error in the LongPressViewController
class.我在
LongPressViewController
类中收到以下错误。
import UIKit
class LongPressViewController: UIViewController {
@IBOutlet weak var imgView: UIImageView!
@IBAction func btnTekan(_ sender: UILongPressGestureRecognizer) {
if sender.state == UIGestureRecognizer.State.began {
let alertController = UIAlertController(title: nil, message: "Long Press terdeteksi", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alertController, animated: true)
}
}
override func viewDidLoad() {
super.viewDidLoad()
let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress(_:)))
}
@objc func handleLongPress(_ recognizer: UILongPressGestureRecognizer){
switch recognizer.state {
case .began:
UIView.animate(withDuration: 0.05,
animations: {
self.imgView.transform = CGAffineTransform(scaleX: 1.5, y: 1.5)
},
completion: nil)
case .ended:
UIView.animate(withDuration: 0.05) {
self.imgView.transform = CGAffineTransform.identity
}
default:
break
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
You should indicate what error is reported on which line of your code.您应该指出在代码的哪一行报告了什么错误。
As is, your code may warn you that the constant longPressGesture
declared and initialized in viewDidLoad()
was never used. longPressGesture
,您的代码可能会警告您从未使用在viewDidLoad()
声明和初始化的常量longPressGesture
。 This is not an error, but just a friendly reminder that this line has no effect.这不是错误,只是友好提醒,此行无效。 You may attach the recognizer to the controlled view by adding the following line:
您可以通过添加以下行将识别器附加到受控视图:
view.addGestureRecognizer(longPressGesture)
Alternatively, you can declare longPressGesture
as an @IBOutlet
and attach it in Interface Builder.或者,你可以声明
longPressGesture
作为@IBOutlet
和界面生成器,将其固定。
Further up where you use State
, the property state
implies already the type UIGestureRecognizer.State
, so you may shorten your line to进一步使用
State
,属性state
已经暗示了UIGestureRecognizer.State
类型,因此您可以将行缩短为
if sender.state == .began {
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.