ios - 类型“UIGestureRecognizer”没有成员“状态”
问题描述
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)
或者,您可以在 Interface Builder 中声明并附加它longPressGesture
。@IBOutlet
在你使用State
的地方,属性state
已经暗示了类型UIGestureRecognizer.State
,所以你可以将你的行缩短到
if sender.state == .began {
推荐阅读
- java - 在没有主机模式的情况下,从 docker 到 localhost 的 mysql 的 jdbc url 不起作用
- unity3d - 购买后换场景
- c# - 按字母顺序排序一个列表,并让其他 3 个列表以相同的顺序重新排序
- javascript - 子组件未随更改重新渲染
- sql - 将 CSV 文件从 SFTP 服务器自动传输到 sql 服务器作为单独的表
- r - dplyr 在 for 循环中发生变异 - 问题
- python - Pyinstaller Packed EXE 无法在其他 PC 上运行脚本。说:Qt5Core,找不到dll
- python - 如何从 Python 3 中的字符串列表中获取时间戳和用户 ID?
- python - Tensorflow InvalidArgumentError 矩阵大小不兼容
- wordpress - Wordpress 小部件中的可点击图像链接