2013-04-07 130 views
0

我有UIImageView裏面的UIScrollview,當我嘗試捏和縮放,圖像跳下來,右(我認爲座標0,0而不是居中),但保持相同的大小,並在同一個地方,直到我停止捏,然後它回到它以前居中的自己。UIScrollView內容不縮放

我得到NSLog在縮放時打印出zoomScale,並且只是保持打印0.5,直到我放開,然後打印1.0一次。

我真的很茫然,所有關於這個的教程看起來非常簡單和容易,我不知道我要去哪裏錯了。

備註: scrollView和ImageView都在storyboard中,連接到插座。它們所在的viewController是一個實現了viewForZoomingInScrollView:的UIScrollView委託。 minimumScale是1.0,maximumScale是4.0,儘管這些數字似乎沒有任何影響。

+0

https://developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/ UIScrollView_pg/ZoomZoom/ZoomZoom.html – Alex 2013-04-07 09:58:08

+0

感謝iOS10,該文檔看起來非常簡單直接,但它怎麼不適合我?我設置委託,並實現viewForZoomingInScrollView :,還有什麼可以做的? – Mirror318 2013-04-07 21:35:23

回答

1

將這個代碼在viewDidLoad中

yourScroll.bouncesZoom = YES; 
yourScroll.delegate = self; 
yourScroll.clipsToBounds = YES; 

UITapGestureRecognizer *twoFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTwoFingerTap:)]; 

[twoFingerTap setNumberOfTouchesRequired:2]; 

[yourImageView addGestureRecognizer:twoFingerTap]; 

float minimumScale = 1.0;//This is the minimum scale, set it to whatever you want. 1.0 = default 

yourScroll.maximumZoomScale = 4.0; 
yourScroll.minimumZoomScale = minimumScale; 
yourScroll.zoomScale = minimumScale; 
[yourScroll setContentMode:UIViewContentModeScaleAspectFit]; 
[yourScroll sizeToFit]; 
[yourScroll setContentSize:CGSizeMake(yourImageView.frame.size.width, yourImageView.frame.size.height)]; 

添加滾動視圖的委託方法和手勢

#pragma mark UIScrollViewDelegate methods 

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView { 

    return yourImageView; 
} 

#pragma mark TapDetectingImageViewDelegate methods 

- (void)scrollViewDidZoom:(UIScrollView *)aScrollView { 
    CGFloat offsetX = (yourScroll.bounds.size.width > yourScroll.contentSize.width)? 
    (yourScroll.bounds.size.width - yourScroll.contentSize.width) * 0.5 : 0.0; 
    CGFloat offsetY = (yourScroll.bounds.size.height > yourScroll.contentSize.height)? 
    (yourScroll.bounds.size.height - yourScroll.contentSize.height) * 0.5 : 0.0; 
    yourImageView.center = CGPointMake(yourScroll.contentSize.width * 0.5 + offsetX, 
             yourScroll.contentSize.height * 0.5 + offsetY); 
} 


- (void)handleTwoFingerTap:(UIGestureRecognizer *)gestureRecognizer { 
    // two-finger tap zooms out 
    float newScale = [previewScroll zoomScale]/ZOOM_STEP; 
    CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gestureRecognizer locationInView:gestureRecognizer.view]]; 
    [yourScroll zoomToRect:zoomRect animated:YES]; 
} 

#pragma mark Utility methods 

- (CGRect)zoomRectForScale:(float)scale withCenter:(CGPoint)center { 

    CGRect zoomRect; 

    // the zoom rect is in the content view's coordinates. 
    // At a zoom scale of 1.0, it would be the size of the imageScrollView's bounds. 
    // As the zoom scale decreases, so more content is visible, the size of the rect grows. 
    zoomRect.size.height = [previewScroll frame].size.height/scale; 
    zoomRect.size.width = [previewScroll frame].size.width/scale; 

    // choose an origin so as to get the right center. 
    zoomRect.origin.x = center.x - (zoomRect.size.width/2.0); 
    zoomRect.origin.y = center.y - (zoomRect.size.height/2.0); 

    return zoomRect; 
} 
+2

這對編程有幫助,但包括Apple自己的文檔在內的每個教程都說我唯一需要做代碼的事情是實現viewForZoomingInScrollView – Mirror318 2013-04-07 21:10:47