UIScrollView 實現完美縮放
阿新 • • 發佈:2019-01-25
UIScrollView實現縮放非常簡單。
1、首先新增代理UIScrollViewDelegate
,並且設定自己建立的UIScrollView物件的代理scrollView.delegate = self;
;
2、定義縮放的最大比例和最小比例;
比如:
#define minScale 1
#define maxScale 2
[_scrollView setMinimumZoomScale:minScale];
[_scrollView setMaximumZoomScale:maxScale];
[_scrollView setZoomScale:minScale animated:YES];
3、實現代理方法
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return scrollVew中要縮放的檢視;
}
// called before the scroll view begins zooming its content縮放開始的時候呼叫
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view NS_AVAILABLE_IOS(3_2){
NSLog (@"%s",__func__);
}
// scale between minimum and maximum. called after any 'bounce' animations縮放完畢的時候呼叫。
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
{
//把當前的縮放比例設進ZoomScale,以便下次縮放時實在現有的比例的基礎上
NSLog(@"scale is %f",scale);
[_scrollView setZoomScale:scale animated:NO ];
}
// 縮放時呼叫
- (void)scrollViewDidZoom:(UIScrollView *)scrollView{
// 可以實時監測縮放比例
NSLog(@"......scale is %f",scrollView.zoomScale);
}