iOS:我可以覆盖UIScrollView的捏合/缩小行为吗? [英] iOS: Can I override pinch in/out behavior of UIScrollView?
问题描述
我在 UIView
上绘制图表,该图表包含在 UIScrollView
中,以便用户可以水平滚动以查看整个图形。
I'm drawing a graph on a UIView
, which is contained by a UIScrollView
so that the user can scroll horizontally to look around the entire graph.
现在我想在用户用两根手指插入时缩放图形,而不是在具有相同的视图中进行缩放对于X和Y方向的速率,我想通过改变X比例来仅在X方向上进行缩放,而不改变Y比例。
Now I want to zoom the graph when a user pinches in with two fingers, but instead of zooming in a view with the same rate for X and Y direction, I want to zoom only in the X direction by changing the X scale, without changing the Y scale.
我想我必须抓住捏合/缩小手势并重绘图形,覆盖默认的缩放行为。
I think I have to catch the pinch in/out gesture and redraw the graph, overriding the default zooming behavior.
但有没有办法做到这一点?
But is there a way to do this?
我一直很难在 UIScrollView
上捕捉捏手势,因为它在开始滚动时取消了触摸。我希望即使在 UIScrollView
取消触摸后,缩放也能正常工作。 :(
I've been having a very difficult time to catch the pinch gesture on the UIScrollView
, as it cancels the touches when it starts to scroll. I want the zooming to work even after the UIScrollView
cancels the touches. :(
谢谢,
Kura
Thanks, Kura
推荐答案
虽然你不能删除现有的捏手势识别器,你可以禁用它然后添加你自己的:
Although you cannot delete the existing pinch gesture recognizer, you can disable it and then add your own:
// Disable existing recognizer
for (UIGestureRecognizer* recognizer in [_scrollView gestureRecognizers]) {
if ([recognizer isKindOfClass:[UIPinchGestureRecognizer class]]) {
[recognizer setEnabled:NO];
}
}
// Add our own
UIPinchGestureRecognizer* pinchRecognizer =
[[UIPinchGestureRecognizer alloc] initWithTarget:self
action:@selector(pinch:)];
[_scrollView addGestureRecognizer:pinchRecognizer];
[pinchRecognizer release];
然后在
- (void) pinch:(UIPinchGestureRecognizer*)recognizer { .. }
使用
[recognizer locationOfTouch:0 inView:..]
[recognizer locationOfTouch:1 inView:..]
来计算如果用户水平或垂直捏住,则输出。
to figure out if the user is pinching horizontally or vertically.
这篇关于iOS:我可以覆盖UIScrollView的捏合/缩小行为吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!