在滚动视图中使用自动布局缩放图像如何居中?
Zoom image using auto layout in scroll view How to center?
我几乎完全从故事板使用自动布局完成缩放,唯一的问题是我的图像在缩放后没有居中。目标是使图片缩放完全符合图片边界,没有黑边。
这是我的约束(它基本上是带有 imageView 的标准 ScrollView):
这是我按顺序完成的。首先我设置 UIScrollViewDelegate
方法:
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return self.fullScreenImageView;
}
然后在图像下载完成后,我更新 height 约束 UIImageView
:
-(void)setupConstraintsToImageSize:(CGSize)imageSize {
[self.imageHConstraint setConstant:imageSize.height];
[self layoutIfNeeded];
}
对于图像约束就像
所以它是这样工作的(抱歉 3MB GIF):
所以它几乎可以正常工作了,但奇怪的是它走到了底部,而不是居中,如何居中?
我发现我必须用方法做某事 -(void)scrollViewDidZoom:(UIScrollView *)scrollView
但我真的不确定我应该在那里设置什么。
我能够通过子类化 UIScrollView 以编程方式完成此操作(子类化不是必需的,但我想要一些可重用的东西)。
我使用了以下约束:
CGRect visibleRect = CGRectMake(0, self.contentInset.top, self.bounds.size.width, self.bounds.size.height - self.contentInset.top - self.contentInset.bottom);
CGRect scaleFrame = AVMakeRectWithAspectRatioInsideRect(_image.size, visibleRect);
_imageViewLeadingConstraint = [NSLayoutConstraint constraintWithItem:_imageView
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self attribute:NSLayoutAttributeLeading
multiplier:1.0 constant:scaleFrame.origin.x];
_imageViewTopConstraint = [NSLayoutConstraint constraintWithItem:_imageView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self attribute:NSLayoutAttributeTop
multiplier:1.0 constant:scaleFrame.origin.y];
_imageViewWidthConstraint = [NSLayoutConstraint constraintWithItem:_imageView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:self attribute:NSLayoutAttributeWidth
multiplier:scaleFrame.size.width / self.bounds.size.width constant:0];
_imageViewHeightConstraint = [NSLayoutConstraint constraintWithItem:_imageView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:self attribute:NSLayoutAttributeHeight
multiplier:scaleFrame.size.height / self.bounds.size.height constant:0];
[self addConstraints:@[_imageViewLeadingConstraint, _imageViewTopConstraint, _imageViewWidthConstraint, _imageViewHeightConstraint]];
注意这里self
是一个UIScrollView。使用您的滚动视图参考代替它。
在scrollViewDidZoom
委托方法中:
CGFloat Ws = self.frame.size.width - self.contentInset.left - self.contentInset.right;
CGFloat Hs = self.frame.size.height - self.contentInset.top - self.contentInset.bottom;
CGFloat W = _imageView.frame.size.width;
CGFloat H = _imageView.frame.size.height;
_imageViewLeadingConstraint.constant = MAX((Ws-W)/2, 0);
_imageViewTopConstraint.constant = MAX((Hs-H)/2, 0);
[self layoutIfNeeded];
设置适当的内容插图。
尤里卡!我设法用 AutoLayout
完全做到了这一点
要实现这一点,需要做的是为中心 Y 约束创建出口,然后在缩放时修改它:
-(void)scrollViewDidZoom:(UIScrollView *)scrollView {
CGFloat diff = self.fullScreenImageView.frame.size.height-self.fullScreenImageView.image.size.height;
if(self.fullScreenImageView.frame.size.height >= self.frame.size.height) {return;}
[self.centerYConstraint setConstant:-diff/2];
}
就是这样。
我几乎完全从故事板使用自动布局完成缩放,唯一的问题是我的图像在缩放后没有居中。目标是使图片缩放完全符合图片边界,没有黑边。
这是我的约束(它基本上是带有 imageView 的标准 ScrollView):
这是我按顺序完成的。首先我设置 UIScrollViewDelegate
方法:
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return self.fullScreenImageView;
}
然后在图像下载完成后,我更新 height 约束 UIImageView
:
-(void)setupConstraintsToImageSize:(CGSize)imageSize {
[self.imageHConstraint setConstant:imageSize.height];
[self layoutIfNeeded];
}
对于图像约束就像
所以它是这样工作的(抱歉 3MB GIF):
所以它几乎可以正常工作了,但奇怪的是它走到了底部,而不是居中,如何居中?
我发现我必须用方法做某事 -(void)scrollViewDidZoom:(UIScrollView *)scrollView
但我真的不确定我应该在那里设置什么。
我能够通过子类化 UIScrollView 以编程方式完成此操作(子类化不是必需的,但我想要一些可重用的东西)。
我使用了以下约束:
CGRect visibleRect = CGRectMake(0, self.contentInset.top, self.bounds.size.width, self.bounds.size.height - self.contentInset.top - self.contentInset.bottom);
CGRect scaleFrame = AVMakeRectWithAspectRatioInsideRect(_image.size, visibleRect);
_imageViewLeadingConstraint = [NSLayoutConstraint constraintWithItem:_imageView
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self attribute:NSLayoutAttributeLeading
multiplier:1.0 constant:scaleFrame.origin.x];
_imageViewTopConstraint = [NSLayoutConstraint constraintWithItem:_imageView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self attribute:NSLayoutAttributeTop
multiplier:1.0 constant:scaleFrame.origin.y];
_imageViewWidthConstraint = [NSLayoutConstraint constraintWithItem:_imageView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:self attribute:NSLayoutAttributeWidth
multiplier:scaleFrame.size.width / self.bounds.size.width constant:0];
_imageViewHeightConstraint = [NSLayoutConstraint constraintWithItem:_imageView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:self attribute:NSLayoutAttributeHeight
multiplier:scaleFrame.size.height / self.bounds.size.height constant:0];
[self addConstraints:@[_imageViewLeadingConstraint, _imageViewTopConstraint, _imageViewWidthConstraint, _imageViewHeightConstraint]];
注意这里self
是一个UIScrollView。使用您的滚动视图参考代替它。
在scrollViewDidZoom
委托方法中:
CGFloat Ws = self.frame.size.width - self.contentInset.left - self.contentInset.right;
CGFloat Hs = self.frame.size.height - self.contentInset.top - self.contentInset.bottom;
CGFloat W = _imageView.frame.size.width;
CGFloat H = _imageView.frame.size.height;
_imageViewLeadingConstraint.constant = MAX((Ws-W)/2, 0);
_imageViewTopConstraint.constant = MAX((Hs-H)/2, 0);
[self layoutIfNeeded];
设置适当的内容插图。
尤里卡!我设法用 AutoLayout
要实现这一点,需要做的是为中心 Y 约束创建出口,然后在缩放时修改它:
-(void)scrollViewDidZoom:(UIScrollView *)scrollView {
CGFloat diff = self.fullScreenImageView.frame.size.height-self.fullScreenImageView.image.size.height;
if(self.fullScreenImageView.frame.size.height >= self.frame.size.height) {return;}
[self.centerYConstraint setConstant:-diff/2];
}
就是这样。