1

I currently have a single page swift application (for iOS) running on a custom view controller. Within that view controller, I've embedded another custom UIView to display some content / handle some user interaction.

I'm wondering if there is a way to intercept a UISwipe, which is registered in the custom view controller, and have swift ignore it if it occurs on the embedded view. I know that for taps, I could set my view controller as the delegate for the tap gesture recognizer and do something like the following:

// not exact syntax
func gestureRecognizer(touch){
     if (touch.view == self.embeddedView || (touch.view.isDescendantOf(embeddedView))!){
           return false
      }
   return true
}

However I can't find any functionality that does the same for swipes. Could someone point me in the right direction?

finn
  • 19
  • 4
  • Its not clear what you want here: should the swipe be ignored only if it *starts* on the embedded view, or should any motion while on that view be ignored? The former is trivial, the later might be more difficult. – Maury Markowitz Feb 15 '17 at 15:44
  • sorry for not being so clear. I would like to ignore the swipe if it starts on the embedded view. – finn Feb 15 '17 at 15:57

3 Answers3

4

Set the Custom View Controller as the delegate of the swipe gesture and override shouldReceiveTouch method.

func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {
     return !CGRectContainsPoint(embeddedView.bounds, touch.locationInView(embeddedView))
}
Sahana Kini
  • 562
  • 2
  • 8
0

swift 5


func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
    return !containerView.bounds.contains(touch.location(in: containerView))
}

nsleche
  • 108
  • 1
  • 7
-1

There is a UISwipeGestureRecognizer wich u can implement like this:

let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(self.swipeRightDetected))
self.embeddedView.addGestureRecognizer(swipeRight)

Look at this answer for more information: https://stackoverflow.com/a/24215844/5664183

Community
  • 1
  • 1
tmiedema
  • 174
  • 7
  • I understand how to implement a swipe gesture recognizer. I'm trying to figure out how to ignore the swipe if it starts on my embedded view. Sorry if that wasn't clear. – finn Feb 15 '17 at 16:00