iPhone:如何在捏缩放 uiscrollview 时重绘子视图
我正在开发一个 iPhone 应用程序,它将多个自定义 UIView 作为子视图放置在 UIScrollView 中。子视图作为透明视图放置在彼此之上,因为每个视图都有自己的绘图例程来跟踪基础视图的各个部分。基本视图是一个 UIImageView,它通常是一个大图像,我希望用户能够平移和放大和缩小。
我遇到的问题是,当我放大和缩小 UIScrollView 时,子视图在用户缩放时不会重新绘制自身。缩放完成后,我可以正确地重新定位和缩放子视图,但用户体验不太理想。我无法找到隐藏或重绘子视图的方法,因为正在缩放子视图以及 ImageView。
有什么想法吗?
谢谢!
这是我已经实现的代码:
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale {
for (UIView *view in subViews)
{
[view updateView:scale];
}
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *) scrollView {
return imageView;
}
I am developing an iPhone app that places multiple custom UIViews as subviews in a UIScrollView. The subviews are placed on top of each other as transparent views as each view has its own drawing routines that traces parts of the base view. The base view is a UIImageView that is typically a large image that I want the user to be able to pan and zoom in and out of.
The problem I am having is that when I zoom in and out of my UIScrollView, the subviews do not redraw themselves while the user is zooming. I can reposition and scale the subviews properly once the zoom is completed, but the user experience is less than desirable. I have not been able to find a way to either hide or redraw the subviews as the zoom is taking place to scale the subviews along with the ImageView.
Any ideas?
thanks!
Here is the code that I have implemented:
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale {
for (UIView *view in subViews)
{
[view updateView:scale];
}
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *) scrollView {
return imageView;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 >=3.2 中现在有:
- (void)scrollViewDidZoom:(UIScrollView *)scrollView
所以在未来的某一天这将是一个明智的解决方案。
In >=3.2 there is now:
- (void)scrollViewDidZoom:(UIScrollView *)scrollView
So some day in the future this will be the sensible solution to this.
您从
viewForZoomingInScrollView:
返回的imageView
将随着视图缩放而调整大小。您可以在该视图中实现setFrame:
或更标准的layoutSubviews
来进行更新。滚动视图的contentSize
也会在缩放过程中不断更新,因此您可以在自定义滚动视图中实现setContentSize:
。The
imageView
that you are returning fromviewForZoomingInScrollView:
will be resized as the view zooms. You could implementsetFrame:
or the more standardlayoutSubviews
in that view to do your updating. ThecontentSize
of the scroll view is also updated continually during zooming, so you could implementsetContentSize:
in a custom scroll view.我不知道实现它的正确方法,但我为您提供了解决方法:
由于
viewForZoomingInScrollView
是在之前调用的缩放和scrollViewDidEndZooming - 之后,您可以使用viewForZoomingInScrollView在单独的线程中调用方法。该方法将每 100 毫秒(或任何其他看起来不错的时间段)检查UIScrollView
的zoomScale
并进行所有需要的更改。在scrollViewDidEndZooming
中,您将停止新方法的执行。I don't know a proper way to implement it, but I have a work-around for you:
Since the
viewForZoomingInScrollView
is called right before the zooming andscrollViewDidEndZooming
- right after, you could use theviewForZoomingInScrollView
to call a method in a separate thread. That method will check thezoomScale
of yourUIScrollView
each 100 milliseconds (or any other period that would look good) and will make all the needed changes. InscrollViewDidEndZooming
you will stop the execution of the new method.