我想在检测到第二次触摸后停止滚动并用我自己的捏合手势处理触摸。 我在滚动视图中尝试过这个:
- (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;
但是什么都没有
我找到解决方案。我重新定义了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;
现在我的捏合手势可以工作了。
您可以使用如下方式禁用滚动视图:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if ([touches count] == 2) {
//Disable scrollview
}
}
将
delayContentTouches
的 UIScrollView
属性设置为 NO(而不是默认的 YES)。这将允许触摸立即传播到滚动视图的 subviews
。
我发现设置 UIPangestureRecognizer 的启用属性不起作用,至少在我的代码中是这样。但是,设置 UIScrollView 的scrollEnabled 属性对我有用。
scrollView.scrollEnabled = false;
scrollView.scrollEnabled = true;
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
}