UIScrollView内容不缩放

时间:2013-04-07 09:44:01

标签: ios xcode uiscrollview zoom

我在UIScrollview中有一个UIImageView,当我尝试捏合和缩放时,图像向下和向右跳跃(我认为是坐标0,0而不是居中)但是保持相同的大小,并且在同一个地方,直到我停止捏,然后它回到它以前的中心自我。

我让NSLog在缩放时打印出zoomScale,并且只保持打印0.5直到我松开然后它打印1.0次。

我真的很茫然,所有这方面的教程都显得那么简单,我不知道我哪里出错了。

注意: scrollView和ImageView都在故事板中,连接到出口。它们所在的viewController是一个UIScrollView委托,它实现了viewForZoomingInScrollView:。 minimumScale是1.0,maximumScale是4.0,尽管这些数字似乎没有任何影响。

1 个答案:

答案 0 :(得分: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)];

添加Scrollview和Gesture的委托方法

#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;
}
相关问题