我已将UIScrollView
内置三个UIScrollViews
并在这三个scollviews内UImageView
(如此答案:https://stackoverflow.com/a/25768875/2911156)。几乎所有东西都可以工作,但是横向模式的放大被打破了。当用户放大图像时,在缩放完成后,图像移动到右边缘并剪切。如何在缩放后居中图像,或者这里出了什么问题?
UI结构:
主滚动视图约束:
图像视图约束(三者都有相同的模式)
我在代码方面做得不多(ScrollViews和ImageViews基于autolayout),只是这样做了:
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return _photoImage;
}
和此:
- (void) setMaxMinZoomScaleForBonds {
self.contentInset = UIEdgeInsetsZero; //need to reset it before load image, because previous insets can be wrong
self.minimumZoomScale = 1.0;
self.zoomScale = 1.0;
self.maximumZoomScale = 2*[UIScreen mainScreen].scale;
}
还做了一件事,但它不起作用:
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale {
//center image when zooming.
CGFloat x = 0;
CGFloat y = 0;
CGFloat screenWidth = [[UIScreen mainScreen] bounds].size.width;
CGFloat screenHight = [[UIScreen mainScreen] bounds].size.height;
CGFloat viewWidth = view.frame.size.width;
CGFloat viewHight = view.frame.size.height;
if(viewWidth < screenWidth)
x = screenWidth/ 2;
if(viewHight < screenHight)
y = screenHight / 2 ;
if(scale<=1.1)
self.contentInset = UIEdgeInsetsZero;
else
self.contentInset = UIEdgeInsetsMake(y, x, y, x);
}
这一切都与缩放动作有关。