override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let rect = CGRect(x: 32, y: 80, width: 256, height: 256)
let imageView = UIImageView(frame: rect)
let image = UIImage(named: "05")
imageView.image = image
imageView.isUserInteractionEnabled = true//开启图像视图对象的交互功能
self.view.addSubview(imageView)
let guesture = UILongPressGestureRecognizer(target: self, action: #selector(ViewController.longPress))//创建一个长按手势对象,用于检测发生在设备中的长按手势
imageView.addGestureRecognizer(guesture)
}
//创建一个方法,用于接收手势事件
@objc func longPress(gusture:UILongPressGestureRecognizer) {
//首先检测一下手势事件的阶段
if(gusture.state == UIGestureRecognizerState.began) {
let alertView = UIAlertController(title: "Information", message: "Long Press", preferredStyle: UIAlertControllerStyle.alert)//当接收到手势事件后,弹出一个窗口
let OKAction = UIAlertAction(title: "OK", style: .default, handler: {_ in})//创建提示窗口的按钮,点击关闭提示窗口
alertView.addAction(OKAction)//将按钮添加到提示窗口中
self.present(alertView, animated: true, completion: nil)//在当前视图控制器中,展示提示窗口
}
}