Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.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
Ios UIScrollView内容未缩放_Ios_Xcode_Uiscrollview_Zooming - Fatal编程技术网

Ios UIScrollView内容未缩放

Ios UIScrollView内容未缩放,ios,xcode,uiscrollview,zooming,Ios,Xcode,Uiscrollview,Zooming,我在UIScrollview中有一个UIImageView,当我尝试收缩和缩放时,图像会向下和向右跳跃(我认为是坐标0,0而不是居中),但保持相同的大小和位置,直到我停止收缩,然后它会弹回到以前居中的状态 我让NSLog在缩放时打印出缩放比例,然后一直打印0.5,直到我放开,然后它打印1.0一次 我真的很茫然,所有关于这方面的教程看起来都很简单,我不知道哪里出了问题 注: scrollView和ImageView都位于故事板中,连接到插座。它们所在的viewController是实现viewFo

我在UIScrollview中有一个UIImageView,当我尝试收缩和缩放时,图像会向下和向右跳跃(我认为是坐标0,0而不是居中),但保持相同的大小和位置,直到我停止收缩,然后它会弹回到以前居中的状态

我让NSLog在缩放时打印出缩放比例,然后一直打印0.5,直到我放开,然后它打印1.0一次

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

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

将此代码放入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和手势的代理方法

#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;
}

感谢iOS10,该文档看起来非常直接和简单,但为什么它对我不起作用?我设置了委托,并实现了viewForZoomingInScrollView:,还有什么要做的呢?这对以编程方式实现这一点很有帮助,但是每个教程(包括Apple自己的文档)都说,我只需要在代码方面实现viewForZoomingInScrollView