用两根手指捏合时禁用 UIScrollView 中的平移

问题描述 投票:0回答:5

我想在检测到第二次触摸后停止滚动并用我自己的捏合手势处理触摸。 我在滚动视图中尝试过这个:

- (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;
}

但这不起作用。

试试这个:

scroll.panGestureRecognizer.maximumNumberOfTouches = 1;

但是什么都没有

objective-c uiscrollview uigesturerecognizer
5个回答
1
投票

我找到解决方案。我重新定义了UIScrollView,并添加:

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

并禁用不可平移手势:

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

现在我的捏合手势可以工作了。


0
投票

您可以使用如下方式禁用滚动视图:

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

       if ([touches count] == 2) {

       //Disable scrollview 
    }
 }

0
投票

delayContentTouches
UIScrollView
属性设置为 NO(而不是默认的 YES)。这将允许触摸立即传播到滚动视图的
subviews


0
投票

我发现设置 UIPangestureRecognizer 的启用属性不起作用,至少在我的代码中是这样。但是,设置 UIScrollView 的scrollEnabled 属性对我有用。

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

0
投票

10年后,我得到了答案。

UIScrollView 将自己设置为委托,然后:

func scrollViewDidZoom(_ scrollView: UIScrollView) {
    drawView.onScale(scale: zoomScale / initScale)
    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
}
© www.soinside.com 2019 - 2024. All rights reserved.