简体   繁体   中英

UIPanGestureRecognizer with UIScrollView

I've been fighting with UIScrollView for a while now. I have a scroll view which has multiple 'card' style views in it. I want the cards to move vertically, as in swiping up and down.

Imagine MobileSafari's tab view, but with swipe down to close tabs.

I can't figure out how to do this without it conflicting with the scroll view horizontal panning. I either get them both panning, or only one panning (vertical / horizontal).

What's the best practice to make this work perfectly, as in "if you're swiping vertically, stop horizontal swiping, and if you're swiping horizontally, stop vertical swiping".

Thank you!

Here's an illustration of what I want :

滑动即可轻扫

The scroll view has its own gesture recognizer: see its panGestureRecognizer property. If you add your own recognizer for detecting the vertical swipe, you can use requireGestureRecognizerToFail: or delegate methods to manage dependencies between the two recognizers.

I'm really confused by the intended behaviour of your app here. You are using swipe and panning interchangeably but they are different gesture recognizers.

Well one way to differentiate is to compare the x and y values of the translationInView: method of the gesture. If y > x, you have a vertical swipe/pan; x > y and you have a horizontal swipe/pan.

Make that gesture recognizer fail if the swipe/pan detected is not the type you are looking out for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM