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