iOS:我可以覆盖 UIScrollView 的收缩/收缩行为吗? [英] iOS: Can I override pinch in/out behavior of UIScrollView?

查看:13
本文介绍了iOS:我可以覆盖 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. :(

谢谢,库拉

推荐答案

虽然不能删除现有的捏合手势识别器,但可以禁用它,然后添加自己的:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆