1. 建立Single View Application專案 ZoomScrollViewTest。
2. 在畫面上添加佔滿螢幕的Scroll View和Image View。
3. 設定圖片的縮放比例
於Scroll View的Attributes Inspector頁面,設定圖片的縮放比例。如上圖所示,我們將Max設為2,如此到時候圖片放大的極限將為Image View尺寸的2倍。Min設為0.5,圖片縮小的極限將為Image View尺寸的一半。
4. 設定ZoomScrollViewTestViewController為scroll view的delegate。
5. 將image view連結至ZoomScrollViewTestViewController,宣告property imageView。
6. 遵從UIScrollViewDelegate protocol。 (ZoomScrollViewTestViewController.h)
@interface ZoomScrollViewTestViewController : UIViewController
<UIScrollViewDelegate>
7. 定義UIScrollViewDelegate的viewForZoomingInScrollView: method。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView | |
{ | |
return imageView; | |
} |
回傳欲縮放的UI元件。就像Alice在童話故事裡被選定具有放大縮小的天命,我們也必須指定誰具有天命。在這裡我們指定熊大王具有縮放的能力。可以放大,熊大王才能夠保護森林,可以縮小,熊大王才可以躲避獵人的追補。若是沒有實作此method,或是回傳nil,到頭來將還是一場空,使用者的手指再怎麼於畫面上縮放,熊大王還是無動於衷。
執行App:
正常尺寸的熊大王
為了保護森林子民放大的熊大王
為了躲避獵人追補縮小的熊大王
ps: 如果想在模擬器上測試縮放,感受熊大王的威風也是ok的。只要按住option鍵,即可以滑鼠模擬縮放手勢。
沒有留言:
張貼留言