How to enable zoom in UIScrollView(如何在 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
UIScrollViewDelegateand is set todelegateon yourUIScrollViewinstance - 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
UIScrollViewinstance, you have to set theminimumZoomScaleand themaximumZoomScaleto 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 中启用缩放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 UIScrollView 中启用缩放
基础教程推荐
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
