我在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.
}
}
最佳答案
警告:您的代码不完整
您应该指出在代码的哪一行报告了什么错误。
照原样,您的代码可能会警告您,从未使用在longPressGesture
中声明和初始化的常量viewDidLoad()
。这不是错误,只是一个友好的提醒,此行无效。您可以通过添加以下行来将识别器附加到受控视图:
view.addGestureRecognizer(longPressGesture)
或者,您可以将
longPressGesture
声明为@IBOutlet
并将其附加到Interface Builder中。在使用
State
的位置上,属性state
已经暗示了类型UIGestureRecognizer.State
,因此可以将行缩短为if sender.state == .began {
关于ios - 类型“UIGestureRecognizer”没有成员“州”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59299376/