問題描述
如何在 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.
這篇關于如何在 UIScrollView 中啟用縮放的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!