0

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

我让 NSLog 在缩放时打印出 zoomScale,并且一直打印 0.5 直到我放手,然后它打印 1.0 一次。

我真的很茫然,所有关于这方面的教程看起来都很基础和简单,我不知道我哪里出错了。

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

4

1 回答 1

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;
}
于 2013-04-07T10:33:33.937 回答