Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem is with UIScrollView only, I also made a rage about it: <a href="https://i.stack.imgur.com/dqx3d.png" rel="noreferrer">http://i.stack.imgur.com/dqx3d.png</a></p> <p>[UPDATE 1] Here the solution:</p> <p>The situation: A UIViewController's view has a UIScrollView as subview (the scroll view paging is disabled).</p> <p>Code I use to attach the gesture the the UIView:</p> <pre><code>UISwipeGestureRecognizer *swipeLeftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipedScreenLeft:)]; [swipeLeftRecognizer setNumberOfTouchesRequired:2.0f]; swipeLeftRecognizer.delegate = self; [swipeLeftRecognizer setDirection:UISwipeGestureRecognizerDirectionLeft]; [self.view addGestureRecognizer:swipeLeftRecognizer]; [swipeLeftRecognizer release]; </code></pre> <p>To be iOS 4.3 enabled, I only had to add the UIViewController as a <code>UIGestureRecognizerDelegate</code></p> <p>Then, I used the following delegate method to intercept and allow the simultaneous recognition of the scrollView's panGesture with the view's swipe one. Here the code:</p> <pre><code>- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{ if ([gestureRecognizer isKindOfClass:[UISwipeGestureRecognizer class]] &amp;&amp; [otherGestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) { return YES; } return NO; } </code></pre> <p>[UPDATE 2]</p> <p>To disable the UIScrollView panning with two fingers, I THINK that you have to make your scrollview, a custom subclass of the UISCrollView class and alter some behavior of the panGesture detector, BUT I didn't tried to do that. Instead I choosed a lazier solution, basically I enable/disable the scrollView scroll functionlity based on the current state of the UISwipeGestureRecognizer. Moreover, in order to prevent double touch movement in the other direction too, I attached another recognizer just for that purpose.</p> <p>You have to create two properties for your swipe detectors.</p> <pre><code>@property (nonatomic,assign) UISwipeGestureRecognizer *swipeRightRecognizer; @property (nonatomic,assign) UISwipeGestureRecognizer *swipeLeftRecognizer; </code></pre> <p>Then I coded like this:</p> <pre><code>[self setSwipeRightRecognizer:[[UISwipeGestureRecognizer alloc] initWithTarget:nil action:nil]]; [swipeRightRecognizer setNumberOfTouchesRequired:2.0f]; swipeRightRecognizer.delegate = self; [swipeRightRecognizer setDirection:UISwipeGestureRecognizerDirectionRight]; [self addObserver:self forKeyPath:@"swipeRightRecognizer.state" options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld) context:NULL]; [self.view addGestureRecognizer:swipeRightRecognizer]; [swipeRightRecognizer release]; [self setSwipeLeftRecognizer:[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipedScreenLeft:)]]; [swipeLeftRecognizer setNumberOfTouchesRequired:2.0f]; swipeLeftRecognizer.delegate = self; [swipeLeftRecognizer setDirection:UISwipeGestureRecognizerDirectionLeft]; [self addObserver:self forKeyPath:@"swipeLeftRecognizer.state" options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld) context:NULL]; [self.view addGestureRecognizer:swipeLeftRecognizer]; [swipeLeftRecognizer release]; </code></pre> <p>then add this method:</p> <pre><code>- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if (self.swipeRightRecognizer.state == UIGestureRecognizerStateFailed) { self.scrollView.scrollEnabled = YES; return; } if ([self.swipeRightRecognizer numberOfTouches] != 2.0f) { self.scrollView.scrollEnabled = YES; } else{ self.scrollView.scrollEnabled = NO; } } </code></pre> <p>And update the existing method I posted in the previous "[UPDATE]":</p> <pre><code>- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{ if ([gestureRecognizer isKindOfClass:[UISwipeGestureRecognizer class]] &amp;&amp; [otherGestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) { if ([gestureRecognizer numberOfTouches] != 2.0f) { self.scrollView.scrollEnabled = YES; } else{ self.scrollView.scrollEnabled = NO; } return YES; } return NO; } </code></pre> <p>Finally, remove the observers in the dealloc:</p> <pre><code>[self removeObserver:self forKeyPath:@"swipeRightRecognizer.state"]; [self removeObserver:self forKeyPath:@"swipeLeftRecognizer.state"]; </code></pre> <p>I bet there is a cleaner solution, but it works.. .</p> <p>Hope it helps ;)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload