RxSwift之UI控件UIGestureRecognizer扩展的使用
【摘要】
RxCocoa 同样对 UIGestureRecognizer 进行了扩展,并增加相关的响应方法。现在以滑动手势为例做具体说明,其它手势用法也是一样的。当手指在界面上向上滑动时,弹出提示框,并显示出滑动起...
- RxCocoa 同样对 UIGestureRecognizer 进行了扩展,并增加相关的响应方法。现在以滑动手势为例做具体说明,其它手势用法也是一样的。
- 当手指在界面上向上滑动时,弹出提示框,并显示出滑动起点的坐标,效果如下:
- 响应回调的示例一:
// 添加一个上滑手势
let swipe = UISwipeGestureRecognizer()
swipe.direction = .up
self.view.addGestureRecognizer(swipe)
// 手势响应
swipe.rx.event
.subscribe(onNext: { [weak self] recognizer in
// 滑动的起点
let point = recognizer.location(in: recognizer.view)
self?.showAlert(title: "向上划动", message: "\(point.x) \(point.y)")
})
.disposed(by: disposeBag)
// 显示消息提示框
func showAlert(title: String, message: String) {
let alert = UIAlertController(title: title, message: message,
preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "确定", style: .cancel))
self.present(alert, animated: true)
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 响应回调的示例二:
// 添加一个上滑手势
let swipe = UISwipeGestureRecognizer()
swipe.direction = .up
self.view.addGestureRecognizer(swipe)
// 手势响应
swipe.rx.event
.bind { [weak self] recognizer in
let point = recognizer.location(in: recognizer.view)
self?.showAlert(title: "向上划动", message: "\(point.x) \(point.y)")
}
.disposed(by: disposeBag)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
文章来源: blog.csdn.net,作者:Serendipity·y,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/Forever_wj/article/details/121308695
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)