ScrollView questions about contentSize zoom function

Recently worked ScrollView zoom function, we found some problems, did some tests, summarized some of it.

    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    _scrollView.maximumZoomScale=5;
    _scrollView.minimumZoomScale=1;
    _scrollView.delegate=self;
    _scrollView.bounces = NO;
    _scrollView.bouncesZoom = NO;

//缩放对应的view
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    //设置这个后,初始化的时候,contentSise会跟imgView.frame.size大小一样,缩放时候,imgView.bound的大小不会改变,只会改变frame
    return _imgView;
}

conclusion as below:

1, start creating ScrollView time, contentSize is 0, the first operation is scaling, contentSize will configure its initial size equal to imgView.frame.size
2, when using the zoom function _scrollView, in fact, scaling is contentSize, At the same time then the corresponding change mgView.frame.size, remember that size does not change imgView.bound, it will only change Frame
3, the contentView based on the current zoom view, change its corresponding contentOffset, to adapt to the view. This is not well understood. view is added to the scollView, not contentView. For example, I would like to view has been scaling in the middle:

- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
    
    CGFloat xcenter = scrollView.width/2 , ycenter = scrollView.height/2;
    self.imgView.center = CGPointMake(xcenter, ycenter);
}

This is indeed a center, but there is a problem: amplified out of bounds to the right and drag down are invalid, picture the feeling of being cut up.
The reason is that, in the contentOffset x, y are 0, the previous operation certainly not slide. Because the view itself is centered on scrollView, after scaling, the position has always been this, fingers still reasonable range, so the content will not be adjusted.
The correct approach is:

- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
    
    CGFloat xcenter = scrollView.width/2 , ycenter = scrollView.height/2;
    xcenter = scrollView.contentSize.width > scrollView.frame.size.width ? scrollView.contentSize.width/2 : xcenter;
    ycenter = scrollView.contentSize.height > scrollView.frame.size.height ? scrollView.contentSize.height/2 : ycenter;
    self.imgView.center = CGPointMake(xcenter, ycenter);
},
这样,感觉是把view靠右边设置了,缩放会一直往右边移动。但运动是相对的,为了使view在缩放手指的范围内,显得不突兀,那么也可以通过调整content的位置,让它往左移动。
4、当view的竖向即将大于scollView的高度后,有可能会有一个向上跳动的动作,有可能是iphoneX才有的吧,暂时未知原因。
5、contentView(不知道这样叫对不对),反正只是用它来控制滑动。默认的位置x=0,y=0。它的缩放,只会按比例往下跟往右延伸

Reproduced in: https: //www.jianshu.com/p/c50aca9036ad

Guess you like

Origin blog.csdn.net/weixin_34198797/article/details/91084942