同步缩放两个UIScrollViews。

时间:2016-01-29 09:19:10

标签: ios uiscrollview zoom

我有两个UIScrollViews。两个滚动视图都包含一个图像。现在,如果我手动放大第一个滚动视图,我想以编程方式缩放第二个滚动视图。我正在使用 - (UIView *)viewForZoomingInScrollView :( UIScrollView *)scrollView方法来手动缩放第一个滚动视图。第二个滚动视图应缩放到相同的点和比例。我怎样才能做到这一点?

3 个答案:

答案 0 :(得分:0)

在UIScrollViewDelegate方法中尝试这样的事情:

override func scrollViewDidScroll(scrollView: UIScrollView){
    otherScrollView.contentOffset = scrollView.contentOffset
    otherScrollView.zoomScale = scrollView.contentOffset
}

答案 1 :(得分:0)

通过使用这两个滚动视图的标记或对象,您可以在viewForZoomingInScrollView方法中识别需要缩放的滚动视图。

e.g

//need to set the tag for scrollviews
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
if(scrollView.tag == 1) 
{
  return firstImageView;
}
 return secondImageView;

}

OR

//need to create the object of used scrollviews
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
if(scrollView == objScrollViewFirst) 

{
  return firstImageView;
}
 return secondImageView;

}

答案 2 :(得分:0)

这样的事情怎么样:

CGRect zoomRect;
zoomRect.size.height = manualScrollView.frame.size.height / manualScrollView.zoomScale;
zoomRect.size.width  = manualScrollView.frame.size.width  / manualScrollView.zoomScale;
zoomRect.origin.x = manualScrollView.center.x - (zoomRect.size.width  / 2.0);
zoomRect.origin.y = manualScrollView.center.y - (zoomRect.size.height / 2.0);

[autoScrollView zoomToRect:zoomRect animated:YES];
[autoScrollView setZoomScale:manualScrollView.zoomScale animated:YES];