美文网首页
UIView 添加手势

UIView 添加手势

作者: 卢业基 | 来源:发表于2021-12-29 08:03 被阅读0次
        func addSwipe(targetView:UIView)->Void{
            let swipeLeft = UISwipeGestureRecognizer()
            swipeLeft.direction = .left
            swipeLeft.addTarget(self, action: #selector(onSwipeEvt(recognizer:)))
            targetView.addGestureRecognizer(swipeLeft)
            
            let swipeRight = UISwipeGestureRecognizer()
            swipeRight.direction = .right
            swipeRight.addTarget(self, action: #selector(onSwipeEvt(recognizer:)))
            targetView.addGestureRecognizer(swipeRight)
        }
        
        @objc func onSwipeEvt(recognizer:UISwipeGestureRecognizer)->Void{
            if recognizer.direction == .right{
                print("右滑")
            }else if recognizer.direction == .left{
                print("左滑")
            }
        }
    

    通过UISwipeGestureRecognizer.direction 来设置滑动的方向,系统定义方向有:

    extension UISwipeGestureRecognizer {
        public struct Direction : OptionSet {
            public init(rawValue: UInt)
            public static var right: UISwipeGestureRecognizer.Direction { get }
            public static var left: UISwipeGestureRecognizer.Direction { get }
            public static var up: UISwipeGestureRecognizer.Direction { get }
            public static var down: UISwipeGestureRecognizer.Direction { get }
        }
    }
    

    相关文章

      网友评论

          本文标题:UIView 添加手势

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