如何在 UIScrollView 中启用缩放
如何在 UIScrollView
中启用缩放功能?
How do I enable zooming in a UIScrollView
?
推荐答案
答案是 这里:
滚动视图还可以处理内容的缩放和平移.当用户做出捏合或张开手势时,滚动视图会调整内容的偏移量和比例.当手势结束时,管理内容视图的对象应根据需要更新内容的子视图.(请注意,手势可以结束并且手指仍可能向下.)在手势进行期间,滚动视图不会向子视图发送任何跟踪调用.
A scroll view also handles zooming and panning of content. As the user makes a pinch-in or pinch-out gesture, the scroll view adjusts the offset and the scale of the content. When the gesture ends, the object managing the content view should update subviews of the content as necessary. (Note that the gesture can end and a finger could still be down.) While the gesture is in progress, the scroll view does not send any tracking calls to the subview.
UIScrollView 类可以有一个必须采用 UIScrollViewDelegate 协议的委托.要使缩放和平移工作,代理必须同时实现 viewForZoomingInScrollView: 和 scrollViewDidEndZooming:withView:atScale:;另外,最大(maximumZoomScale)和最小(minimumZoomScale)缩放比例必须不同.
The UIScrollView class can have a delegate that must adopt the UIScrollViewDelegate protocol. For zooming and panning to work, the delegate must implement both viewForZoomingInScrollView: and scrollViewDidEndZooming:withView:atScale:; in addition, the maximum (maximumZoomScale) and minimum (minimumZoomScale) zoom scale must be different.
所以:
- 您需要一个实现
UIScrollViewDelegate
并在您的UIScrollView
实例上设置为delegate
的委托 - 在您的委托上,您必须实现一种方法:
viewForZoomingInScrollView:
(必须返回您对缩放感兴趣的内容视图).您还可以选择实现scrollViewDidEndZooming:withView:atScale:
. - 在您的
UIScrollView
实例上,您必须将minimumZoomScale
和maximumZoomScale
设置为不同(默认为 1.0).
- You need a delegate that implements
UIScrollViewDelegate
and is set todelegate
on yourUIScrollView
instance - On your delegate you have to implement one method:
viewForZoomingInScrollView:
(which must return the content view you're interested in zooming). You can also implementscrollViewDidEndZooming:withView:atScale:
optionally. - On your
UIScrollView
instance, you have to set theminimumZoomScale
and themaximumZoomScale
to be different (they are 1.0 by default).
注意:有趣的是,如果您想打破缩放.viewForZooming...
方法中返回 nil
是否足够?它确实打破了缩放,但一些手势会被弄乱(两根手指).因此,要中断缩放,您应该将最小和最大缩放比例设置为 1.0.
Note: The interesting thing about this is what if you want to break zooming. Is it enough to return nil
in the viewForZooming...
method? It does break zooming, but some of the gestures will be messed up (for two fingers). Therefore, to break zooming you should set the min and max zoom scale to 1.0.
相关文章