I'm using a number of
UISwipeGestureRecognizer
var rightSwipe = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipe1"))
rightSwipe.direction = .right
view.addGestureRecognizer(rightSwipe)
isUserInteractionEnabled
for UIImage
and UILabel
is false by default. Set it to true.
As I know, you can't limit gesture area. But you can check location of gesture and decide what to do with it.
For example:
func addGesture() {
let rightGesture = UISwipeGestureRecognizer(target: self, action: #selector(someAction(sender:)))
rightGesture.direction = .right
let leftGesture = UISwipeGestureRecognizer(target: self, action: #selector(someAction(sender:)))
leftGesture.direction = .right
view.addGestureRecognizer(rightGesture)
view.addGestureRecognizer(leftGesture)
}
func someAction(sender: UISwipeGestureRecognizer) {
let location = sender.location(in: view)
if location.x > view.frame.width / 2 && sender.direction == .right {
// do something
} else if location.x < view.frame.width / 2 && sender.direction == .left {
// do something else
}
}