如何在 UIScrollView 中启用缩放

How to enable zoom in UIScrollView(如何在 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.

所以:

  1. 您需要一个实现 UIScrollViewDelegate 并在您的 UIScrollView 实例上设置为 delegate 的委托
  2. 在您的委托上,您必须实现一种方法:viewForZoomingInScrollView:(必须返回您对缩放感兴趣的内容视图).您还可以选择实现 scrollViewDidEndZooming:withView:atScale:.
  3. 在您的 UIScrollView 实例上,您必须将 minimumZoomScalemaximumZoomScale 设置为不同(默认为 1.0).
  1. You need a delegate that implements UIScrollViewDelegate and is set to delegate on your UIScrollView instance
  2. On your delegate you have to implement one method: viewForZoomingInScrollView: (which must return the content view you're interested in zooming). You can also implement scrollViewDidEndZooming:withView:atScale: optionally.
  3. On your 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 中启用缩放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

UIButtons at the bottom of UIScrollView not working(UIScrollView 底部的 UIButtons 不起作用)
scrollViewWillEndDragging:withVelocity:targetContentOffset: not working on the edges of a UISCrollView(scrollViewWillEndDragging:withVelocity:targetContentOffset: 不在 UISCrollView 的边缘工作)
ImageView Scaling when scrolling down(向下滚动时 ImageView 缩放)
Bounds automatically changes on UIScrollView with content insets(UIScrollView 上的边界自动更改,带有内容插图)
iOS5 UITapRecognizer for UIScrollView interfering with buttons. How to fix?(用于 UIScrollView 的 iOS5 UITapRecognizer 干扰按钮.怎么修?)
How to Cancel Scrolling in UIScrollView(如何在 UIScrollView 中取消滚动)