0

UIImageView をズームしようとしていUIScrollViewます。そして、私はこれを持っています:

-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return imageView;
}

-(void)scrollViewDidZoom:(UIScrollView *)scrollView {
[imageView setCenter:CGPointMake(scrollView.center.x, scrollView.center.y)];
}

-(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale {
}

問題は次のとおりです。画像をズームアウトすると中央に表示されますが、ズームインすると画像が左上隅に移動します。

私のコードのどこに問題がありますか?

4

1 に答える 1

3

答えは、からすべて削除しscrollViewDidZoom:て追加することです:

[self centerScrollViewContent];

メソッドの実装:

- (void)centerScrollViewContents {
CGSize boundsSize = self.scrollView.bounds.size;
CGRect contentsFrame = self.containerView.frame;

if (contentsFrame.size.width < boundsSize.width) {
    contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0f;
} else {
    contentsFrame.origin.x = 0.0f;
}

if (contentsFrame.size.height < boundsSize.height) {
    contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0f;
} else {
    contentsFrame.origin.y = 0.0f;
}

self.containerView.frame = contentsFrame;
}
于 2012-11-01T20:30:17.097 に答える