同时缩放两个UIImageView

时间:2017-01-31 21:12:39

标签: objective-c uiscrollview uiimageview uiscrollviewdelegate

我需要有两个UIImageView,都启用了缩放功能(我可以使用典型的UISCrollView技术),每当我放大其中一个时,也会放大另一个,在同一点和相同的缩放。

以下是一段解说视频:https://youtu.be/Ft-Dt3fx3z8?t=29s

你知道任何解决方案吗?

由于

1 个答案:

答案 0 :(得分:0)

我将回答我自己的问题,首先是解决方案缩放异步而不滚动

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    if (scrollView == _scrollView1)
        return self.imageView1;
    else
        return self.imageView2;
}
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
{
    if (scrollView == _scrollView1){
        [_scrollView2 setZoomScale:scale animated:YES];
        [_scrollView2 setContentOffset:_scrollView1.contentOffset animated:YES];
    }
    else if (scrollView == _scrollView2){
        [_scrollView1 setZoomScale:scale animated:YES];
        [_scrollView1 setContentOffset:_scrollView2.contentOffset animated:YES];
    }
}

其次,实时缩放和滚动更好的解决方案(仍然需要viewForZoomingInScrollView)

- (void)matchScrollView:(UIScrollView *)first toScrollView:(UIScrollView *)second {
    CGPoint offset = first.contentOffset;
    offset.x = second.contentOffset.x;
    offset.y = second.contentOffset.y;
    [first setContentOffset:offset];
    [first setZoomScale:second.zoomScale];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if([scrollView isEqual:_scrollView1]) {
        [self matchScrollView:_scrollView2 toScrollView:_scrollView1];
    } else {
        [self matchScrollView:_scrollView1 toScrollView:_scrollView2];
    }
}