Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 如何在ScrollView中居中显示UIImageView(缩放)_Objective C_Uiscrollview_Uiimageview_Zooming - Fatal编程技术网

Objective c 如何在ScrollView中居中显示UIImageView(缩放)

Objective c 如何在ScrollView中居中显示UIImageView(缩放),objective-c,uiscrollview,uiimageview,zooming,Objective C,Uiscrollview,Uiimageview,Zooming,我正在尝试在UIScrollView中缩放UIImageView。我有这个: -(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView { return imageView; } -(void)scrollViewDidZoom:(UIScrollView *)scrollView { [imageView setCenter:CGPointMake(scrollView.center.x, scrollView.cent

我正在尝试在
UIScrollView
中缩放UIImageView。我有这个:

-(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 {
}
问题是:当我缩小图像时,它是居中的,但当我放大时,图像会移到左上角


我的代码哪里有问题?

答案是从
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;
}
-(无效)centerScrollViewContents{
CGSize boundsSize=self.scrollView.bounds.size;
CGRect contentsFrame=self.containerView.frame;
if(contentsFrame.size.width
答案是从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;
}
-(无效)centerScrollViewContents{
CGSize boundsSize=self.scrollView.bounds.size;
CGRect contentsFrame=self.containerView.frame;
if(contentsFrame.size.width