在滚动视图中使用自动布局缩放图像,如何居中?

4
我已经几乎完成了使用自动布局完全从Storyboard缩放的功能,唯一的问题是我缩放后的图像没有居中。目标是使图片缩放到边界,而不留黑色条纹。
以下是我的约束条件(基本上是带有imageView的标准ScrollView): enter image description here 这是我所做的。首先,我设置了UIScrollViewDelegate方法:
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    return self.fullScreenImageView;
}

当图片下载完成后,我会更新UIImageView高度约束

-(void)setupConstraintsToImageSize:(CGSize)imageSize {
    [self.imageHConstraint setConstant:imageSize.height];
    [self layoutIfNeeded];
}

对于图像约束,就像这样:

enter image description here

所以它的工作方式是这样的(抱歉3MB GIF):

enter image description here

所以它几乎ALMOST可以工作,但奇怪的是它向下移动,而不是居中,如何将其居中?

我发现我必须在方法-(void)scrollViewDidZoom:(UIScrollView *)scrollView中做一些事情,但我真的不确定应该设置什么。

2个回答

2

幸运!我完全使用AutoLayout实现了这个。

要达到这个效果,需要创建一个指向你的中心Y约束的outlet,然后在缩放时修改它:

-(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];
}

就是这样了。


1

我能够通过子类化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];

设置适当的内容插入。


谢谢,这给了我灵感,可以在Storyboard中完成这个操作(+1) - Jakub

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接