Swift-Xib上的简练手势事件
作者:
查理布朗Bio | 来源:发表于
2022-12-07 14:04 被阅读0次
前言
- 相信在Xib文件中,通过
UIButton
来拖拽快捷生成@IBAction点击事件
大家都不陌生,但是当我们面对UIView
想要添加手势事件
时却手足无措,这时多半会在代码中单独为控件添加手势事件代码,像下面这样,看着笨重可读性又差,能不能像UIButton
一样方便且简练呢,下面我们来试试通过xib的设置手势事件
@IBOutlet weak var vTarget: UIView!
lazy var gesture: UITapGestureRecognizer = {
return UITapGestureRecognizer(target: self, action: #selector(gestureAction(_:)))
}()
@objc func gestureAction(_ sender: UIView) {
// 事件代码
}
vTarget.addGestureRecognizer(gesture)
流程
- 可以看到xib已经预留了多种手势识别器,这里我们选择一个点击手势

插入手势对象

关联手势事件

设置手势名及传入类型
- 最后拖拽需要响应的该手势的
UIView
与手势事件
进行绑定即可

关联UI与手势对象
本文标题:Swift-Xib上的简练手势事件
本文链接:https://www.haomeiwen.com/subject/fydnfdtx.html
网友评论