Gralex
Gralex

Reputation: 4485

Disable pan in UIScrollView on pinch by 2 fingers

I want to stop scrolling after detect second touch and handle touches with my own pinch gesture. I've tryed this in scroll view:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    if(event.allTouches.count > 2)self.panGestureRecognizer.enabled = NO;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    if(event.allTouches.count > 2)self.panGestureRecognizer.enabled = YES;
}

But it's doesnt works.

Try this:

scroll.panGestureRecognizer.maximumNumberOfTouches = 1;

But nothing

Upvotes: 3

Views: 2878

Answers (5)

iCeors
iCeors

Reputation: 1

After 10 years, I have got the answer.

UIScrollView sets itself as delegate, then:

func scrollViewDidZoom(_ scrollView: UIScrollView) {
    guard pinchCenter != .zero else { return }
    contentOffset.x = pinchStartContentOffset.x - (pinchCenter.x - pinchStartContentOffset.x) * (zoomScale / pinchStartScale - 1)
    contentOffset.y = pinchStartContentOffset.y - (pinchCenter.y - pinchStartContentOffset.y) * (zoomScale / pinchStartScale - 1)
    pinchStartScale = zoomScale
    pinchStartContentOffset = contentOffset
}

var pinchCenter: CGPoint = .zero
var pinchStartScale: CGFloat = 0
var pinchStartContentOffset: CGPoint = .zero

func scrollViewWillBeginZooming(_ scrollView: UIScrollView, with view: UIView?) {
    guard pinchGestureRecognizer?.numberOfTouches ?? 0 >= 2 else { return }
    guard
        let p1 = pinchGestureRecognizer?.location(ofTouch: 0, in: self),
        let p2 = pinchGestureRecognizer?.location(ofTouch: 1, in: self)
    else { return }
    pinchCenter = CGPoint(x: -((p1.x + p2.x) / 2 - bounds.origin.x),
                          y: -((p1.y + p2.y) / 2 - bounds.origin.y))
    pinchStartScale = zoomScale
    pinchStartContentOffset = contentOffset
}

Upvotes: 0

R. Rincón
R. Rincón

Reputation: 361

I found that setting the enabled property of UIPangestureRecognizer didn't work, at least in my code. However, setting the UIScrollView's scrollEnabled property has worked for me.

scrollView.scrollEnabled = false;
scrollView.scrollEnabled = true;

Upvotes: 0

Gralex
Gralex

Reputation: 4485

I find solution. I redefined UIScrollView, and add:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

And disable\enable pan gesture:

if(pinch.state == UIGestureRecognizerStateBegan) scroll.panGestureRecognizer.enabled = NO; 
if(pinch.state == UIGestureRecognizerStateEnded) scroll.panGestureRecognizer.enabled = YES; 

Now my pinch gesture works.

Upvotes: 1

Abid Hussain
Abid Hussain

Reputation: 1549

Set delayContentTouches property of UIScrollView to NO (instead of the default YES). This will allow the touch to propagate to the subviews of the scroll view immediately.

Upvotes: 0

Hemant Dixit
Hemant Dixit

Reputation: 1145

You can disable scroll View using like this :

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

       if ([touches count] == 2) {

       //Disable scrollview 
    }
 }

Upvotes: 0

Related Questions