实现UIScrollView的自定义缩放 [英] Implement custom zooming for a UIScrollView

查看:158
本文介绍了实现UIScrollView的自定义缩放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UIScrollView要求,当缩放时,contentSize.height应该保持不变。从200x100放大应该导致一个新的contentSize例如400x100而不是400x200。我想在用户缩放时做自己的绘图。



我不认为我可以使用UIScrollView的正常缩放行为来实现这一点,所以我我试图卷我自己。 (我可以让它做它的事情,然后重新绘制我的内容,当-scrollViewDidEndZooming:withView:atScale:get被调用,但是不会很漂亮)。



目前我是UIScrollView的子类,并试图做两个手指在屏幕上时我自己的缩放:

   - (void)touchesBegan: (NSSet *)触发事件:(UIEvent *)事件{
if([touches count]!= 2){
[super touchesMoved:touches withEvent:event];
} else {
//做我自己的东西
}
}


b $ b

我想通过覆盖touchesBegan:withEvent :, touchesMoved:withEvent :, touchesEnded:withEvent:和touchesCancelled:withEvent:这样应该工作,但它不工作。



更早的失败尝试是在scrollview的顶部放置一个透明视图,并将不感兴趣的触摸发送到scrollview:

   - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if([touches count]!= 2){
[self .theScrollView touchesMoved:touches withEvent:event];
} else {
//做我自己的东西
}
}



解决方案

如果您尝试在缩放缩放事件的每一帧上重绘内容,您可能无法在缩放期间维持正常的效果。我建议采取让UIScrollView缩放绘图的缩放版本的方法,然后在-scrollViewDidEndZooming:withView:atScale:delegate方法中重绘内容在缩放结束时变得尖锐。这是我在我的应用程序中做的,它最终工作得很好。



在缩放结束时,有一些技巧可以正确调整内容视图我在此答案中说明。基本上,您需要截取到内容视图的转换设置,以便您可以在重新绘制内容时将其设置为1的比例因子。您还需要跟踪该缩放因子,因为UIScrollView没有,并使用该缩放因子调整UIScrollView尝试应用于您的内容视图随后的缩放操作的变换。



您可以使用此技术的修改来强制在捏缩放期间重新绘制您的内容,但在我的测试中,这样做太过急,无法提供良好的用户体验。


I have a UIScrollView with the requirement that, when zooming, the contentSize.height should remain the same. Zooming in from 200x100 should result in a new contentSize of 400x100 instead of 400x200, for instance. I'd like to do my own drawing while the user is zooming.

I don't think I can use the normal zooming behaviour of UIScrollView to achieve this, so I'm trying to roll my own. (I could just let it do its thing and then redraw my contents when -scrollViewDidEndZooming:withView:atScale: gets called, but that wouldn't be very pretty).

Currently I am subclassing UIScrollView and trying to do my own zooming when two fingers are on the screen:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    if ([touches count] != 2) {
        [super touchesMoved:touches withEvent:event];
    } else {
     // do my own stuff
    }
}

I thought that by overriding touchesBegan:withEvent:, touchesMoved:withEvent:, touchesEnded:withEvent: and touchesCancelled:withEvent: in this way should work, but it doesn't.

An earlier failed attempt was to place a transparent view on top of the scrollview and send touches that I'm not interested in to the scrollview :

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    if ([touches count] != 2) {
        [self.theScrollView touchesMoved:touches withEvent:event];
    } else {
     // do my own stuff
    }
}

Any help would be appreciated.

Thanks, Thomas

解决方案

You probably won't be able to maintain decent performance during zooming if you attempt to redraw your content on every frame of a pinch-zooming event. I'd recommend taking the approach of letting the UIScrollView zoom a scaled version of your drawing in or out, then redraw the content to be sharp at the end of the zoom in the -scrollViewDidEndZooming:withView:atScale: delegate method. This is what I do in my application, and it ends up working very well.

There are some tricks to resizing your content view properly at the end of a zoom, which I describe in this answer. Basically, you need to intercept the setting of a transform to your content view so that you can set it to a scale factor of 1 when you redraw your content. You'll also need to keep track of that scale factor, because the UIScrollView doesn't, and use that scale factor to adjust the transform that UIScrollView tries to apply to your content view with subsequent zoom operations.

You could use a modification of this technique to force a redraw of your content during the pinch-zooming, but in my tests this ended up being far too jerky to provide a good user experience.

这篇关于实现UIScrollView的自定义缩放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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