如何在 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).UIScrollViewDelegate and is set to delegate on your UIScrollView instanceviewForZoomingInScrollView: (which must return the content view you're interested in zooming). You can also implement scrollViewDidEndZooming:withView:atScale: optionally.UIScrollView instance, you have to set the minimumZoomScale and the maximumZoomScale 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.
这篇关于如何在 UIScrollView 中启用缩放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
如何继承 UIScrollView 并使委托属性私有How to subclass UIScrollView and make the delegate property private(如何继承 UIScrollView 并使委托属性私有)
Swift - 如何从照片库中获取最后拍摄的 3 张照片Swift - how to get last taken 3 photos from photo library?(Swift - 如何从照片库中获取最后拍摄的 3 张照片?)
以编程方式设置 contentOffset 触发 scrollViewDidScrolSetting contentOffset programmatically triggers scrollViewDidScroll(以编程方式设置 contentOffset 触发 scrollViewDidScroll)
使用 pagingEnabled 的 UIScrollView 中页面之间的照片应Photos app-like gap between pages in UIScrollView with pagingEnabled(使用 pagingEnabled 的 UIScrollView 中页面之间的照片应用程序式间隙
为什么 UIScrollView 在 ios 6 和 ios 7 中从顶部留下空why UIScrollView is leaving space from top in ios 6 and ios 7(为什么 UIScrollView 在 ios 6 和 ios 7 中从顶部留下空间)
UIScrollView 在滚动时暂停 NSTimerUIScrollView pauses NSTimer while scrolling(UIScrollView 在滚动时暂停 NSTimer)