Xcode9学习笔记13 - 手势检测之长按

本文记录了在Xcode9中如何实现长按手势检测的详细步骤,包括手势识别器的添加、事件处理及实际应用示例,帮助开发者提升iOS应用的交互体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


    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)//在当前视图控制器中,展示提示窗口
        }
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值