0

UIScrollview内にUIImageViewがあり、ピンチしてズームしようとすると、画像が上下左右にジャンプします(中央ではなく0,0を座標にすると思います)が、停止するまで同じサイズと同じ場所に留まりますピンチすると、元の中心に戻ってきます。

ズーム中にzoomScaleを出力するようにNSLogを取得し、手放すまで0.5を出力し続け、その後1.0を1回出力しました。

私はここで本当に途方に暮れています.これに関するすべてのチュートリアルはとても基本的で簡単に思えます.どこが間違っているのかわかりません.

注: scrollView と ImageView の両方がストーリーボードにあり、アウトレットに接続されています。それらが含まれているviewControllerは、viewForZoomingInScrollView:を実装するUIScrollViewデリゲートです。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 に答える