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()
}
}
网友评论