是否有可能在UIScrollView放大和缩小UIImageView,但坚持自动布局居中?
长话短说,我正在尝试构build类似于Photos.app的function。
我有一个UIImageView里面的UIScrollView设置在故事板中。 缩放的作品,但我无法保持居中。 在我所有基于框架的滚动视图实现中,我已经将它集中为如下,这很好地工作:
- (void)scrollViewDidZoom:(UIScrollView *)scrollView { CGRect newImageViewFrame = self.imageView.frame; // Center horizontally if (newImageViewFrame.size.width < CGRectGetWidth(scrollView.bounds)) { newImageViewFrame.origin.x = (CGRectGetWidth(scrollView.bounds) - CGRectGetWidth(self.imageView.frame)) / 2; } else { newImageViewFrame.origin.x = 0; } // Center vertically if (newImageViewFrame.size.height < CGRectGetHeight(scrollView.bounds)) { newImageViewFrame.origin.y = (CGRectGetHeight(scrollView.bounds) - CGRectGetHeight(self.imageView.frame)) / 2; } else { newImageViewFrame.origin.y = 0; } self.imageView.frame = newImageViewFrame; }
但是使用自动布局并不是。
我没有对UIScrollView或UIImageView的约束,因为我不知道他们应该是什么。 我想我应该将UIScrollView粘贴到四个angular落,但是对于UIImageView我并不完全确定,因为缩放会改变其框架。
这是一个示例项目: http : //cl.ly/21371H3q381N
我将如何去放大与自动布局居中工作?
当使用Autolayout对setFrames的调用没有影响,这就是为什么imageView不居中在您的scrollView。
这就是说,为了达到中心效果,你可以select:
-
最简单的方法是在
ViewDidLoad
为您的ViewDidLoad
设置translatesAutoresizingMaskIntoConstraints
为YES
,这将根据您的imageView autoresizingMask将调用转换为setFrame:
新约束。 但是你应该确保新的约束条件满足你在故事板中设置的约束(在你的情况下没有)。 -
在你的
scrollViewDidZoom:
方法中,直接添加约束来居中你的scrollViewDidZoom:
–
[self.imageView addConstraint:[NSLayoutConstraint constraintWithItem:self.imageView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]]; [self.imageView addConstraint:[NSLayoutConstraint constraintWithItem:self.imageView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.scrollView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];
要将内容居中,可以参考以下代码片段:
// To re-center the image after zooming in and zooming out - (void)centerScrollViewContents { CGSize boundsSize = self.bounds.size; CGRect contentsFrame = self.imageView.frame; if (contentsFrame.size.width < boundsSize.width) { contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width) / 2.0f; } else { contentsFrame.origin.x = 0.0f; } if (contentsFrame.size.height < boundsSize.height) { contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height) / 2.0f; } else { contentsFrame.origin.y = 0.0f; } self.imageView.frame = contentsFrame; } //for zooming in and zooming out - (void)scrollViewDidZoom:(UIScrollView *)scrollView { if (self.zoomScale > 1.0f) { [self centerScrollViewContents]; } else { self.bouncesZoom = NO; // for zooming out by pinching [self centerScrollViewContents]; } }
通过这种方式,您可以放大和缩小图像的坐标,而不用AutoLayout。 请让我知道,如果有帮助。 谢谢 :)
- 只在iPhone中查看不显示在iPad中
- AutoLayout:UIImageView和Aspect Fit内容模式
- 首次出现时不能更改UITableViewCell中的UILabel的框架(使用Autolayout)
- UISplitViewController – 设置始终可见的主控制器
- 如何将宽度等同于NSLayoutConstraint的某些UIViews?
- UICollectionViewCell systemLayoutSizeFittingSize返回不正确的宽度
- 多行UILabel与UIScrollview使用自动布局
- 使用自定义animation时,会在iOS9上忽略edgesForExtendedLayout
- 如何设置子视图控制器的topLayoutGuide位置