美文网首页
UIButton 长按事件

UIButton 长按事件

作者: 咸鱼有只喵 | 来源:发表于2018-08-07 15:48 被阅读13次


    let aBtn = UIButton(type: .custom)
    aBtn.frame = CGRect(x: 40, y: 100, width: 60, height: 60)
    aBtn.setBackgroundImage(UIImage(named: "111.png"), for: .normal)
    //button点击事件
    aBtn.addTarget(self, action: #selector(self.btnShort(_:)), for: .touchUpInside)
        //button长按事件
    let longPress = UILongPressGestureRecognizer(target: self, action: #selector(self.btnLong(_:)))
    longPress.minimumPressDuration = 0.8
    //定义按的时间
    aBtn.addGestureRecognizer(longPress)


@objc func btnLong(_ gestureRecognizer: UILongPressGestureRecognizer?) {

    if gestureRecognizer?.state == UIGestureRecognizerState.began {
        print("长按事件")
        let alert = UIAlertView(title: "消息", message: "确定删除该模式吗?", delegate: self, cancelButtonTitle: "取消", otherButtonTitles: "删除")
        alert.show()
    }
}

相关文章

网友评论

      本文标题:UIButton 长按事件

      本文链接:https://www.haomeiwen.com/subject/gglavftx.html