Swift 简单封装UISwipeGestureRecognizer + 闭包回调监听事件,便于不同场景的 UIView调用

前端之家收集整理的这篇文章主要介绍了Swift 简单封装UISwipeGestureRecognizer + 闭包回调监听事件,便于不同场景的 UIView调用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. typealias sendValueClosure = (swipeGesture: UISwipeGestureRecognizer) -> Void</span>
  2. class SwipeGesture:NSObject {
  3. //声明一个闭包
  4. var myClosure: sendValueClosure?
  5. //下面的方法需要传入上个界面的someFunctionThatAClosure函数指针
  6. func initWithClosure(closuer: sendValueClosure?) {
  7. //讲函数指针赋值给myClosure闭包,该闭包中覆盖了someFunctionThatTakesAClosure函数中的局部变量等引用
  8. self.myClosure = closuer
  9. }
  10. func setSwipeGesture(view:UIView){
  11. //设置手势
  12. let swipeRight = UISwipeGestureRecognizer(target: self,action: "respondToSwipeGesture:")
  13. swipeRight.direction = UISwipeGestureRecognizerDirection.Right
  14. view.addGestureRecognizer(swipeRight)
  15. let swipeLeft = UISwipeGestureRecognizer(target: self,action: "respondToSwipeGesture:")
  16. swipeLeft.direction = UISwipeGestureRecognizerDirection.Left
  17. view.addGestureRecognizer(swipeLeft)
  18. let swipeUp = UISwipeGestureRecognizer(target: self,action: "respondToSwipeGesture:")
  19. swipeUp.direction = UISwipeGestureRecognizerDirection.Up
  20. view.addGestureRecognizer(swipeUp)
  21. let swipeDown = UISwipeGestureRecognizer(target: self,action: "respondToSwipeGesture:")
  22. swipeDown.direction = UISwipeGestureRecognizerDirection.Down
  23. view.addGestureRecognizer(swipeDown)
  24. }
  25. //事件
  26. func respondToSwipeGesture(gesture: UIGestureRecognizer){
  27. if let swipeGesture = gesture as? UISwipeGestureRecognizer {
  28. if let closure = self.myClosure {
  29. closure(swipeGesture: swipeGesture)
  30. }
  31. }
  32. }
  33. }
这个类功能就是封装UISwipeGestureRecognizer,添加上下左右4种滑动手势及相应的时间,这些都不重要,重要的是:
  1. //声明一个闭包
  2. var myClosure: sendValueClosure?
  3. //下面的方法需要传入上个界面的someFunctionThatAClosure函数指针
  4. func initWithClosure(closuer: sendValueClosure?) {
  5. //讲函数指针赋值给myClosure闭包,该闭包中覆盖了someFunctionThatTakesAClosure函数中的局部变量等引用
  6. self.myClosure = closuer
  7. }

声明的闭包myClosure在类的最上面那句代码

  1. typealias sendValueClosure = (swipeGesture: UISwipeGestureRecognizer) -> Void
initWithClosure(closure: sendValueClosure)这个函数是被我们UIView源文件调用的。

下面respondToSwipeGesture(gesture:UIGestureRecognizer)事件触发的时候,里面的

  1. if let closure = self.myClosure {
  2. closure(swipeGesture: swipeGesture)
  3. }

是把 UISwipeGestureRecognizer 当做参数返回给闭包。

下面就是我们的 UIView 源文件调用啦:

  1. //实例化类
  2. let mySwipeGesture = SwipeGesture()
  3. // 给 UIView 添加手势
  4. mySwipeGesture.setSwipeGesture( yourView)
  5. //调用闭包监听事件
  6. mySwipeGesture.initWithClosure { (swipeGesture) -> Void in
  7. switch swipeGesture.direction {
  8. case UISwipeGestureRecognizerDirection.Right:
  9. print("Swiped right")
  10. case UISwipeGestureRecognizerDirection.Left:
  11. print("Swiped left")
  12. case UISwipeGestureRecognizerDirection.Up:
  13. print("Swiped up")
  14. case UISwipeGestureRecognizerDirection.Down:
  15. print("Swiped down")
  16. default:
  17. break
  18. }
  19. }
是的,就是这么简单。。手势不是目的,封装不是目的,目的是学会用闭包实现数据监听及传送数据。

猜你在找的Swift相关文章