iOS - UIScrollView 可以滚动但不能缩放

Posted

技术标签:

【中文标题】iOS - UIScrollView 可以滚动但不能缩放【英文标题】:iOS - UIScrollView can scroll but cannot zoom 【发布时间】:2016-03-06 14:49:41 【问题描述】:

我想放大/缩小包含UIImageViewUIScrollView。使用下面的代码,我只能滚动滚动视图内容,但不能缩放它。

我的视图层次结构如下所示:

- (UIView *) containerView
-- (UIView *) contentView
--- (UIScrollView *) scrollView
---- (UIImageView *) self.imageView

代码:

UIView *previousContentView = nil;

        for (NSInteger i = 0; i < 2; i++) 
            contentView = [self addRandomColoredView];
            [containerView addSubview:contentView];
            [containerView.topAnchor constraintEqualToAnchor:contentView.topAnchor].active = true;
            [containerView.bottomAnchor constraintEqualToAnchor:contentView.bottomAnchor].active = true;

            scrollView = [self addRandomScrollView];
            [contentView addSubview:scrollView];

            self.imageView = [[UIImageView alloc] init];
            [self.imageView setImage:[imagesArray objectAtIndex:i]];
            [self.imageView setTranslatesAutoresizingMaskIntoConstraints:false];
            [scrollView addSubview:self.imageView];

            if (previousContentView) 
                [VerticalSeparatorView addSeparatorBetweenView:previousContentView secondView:contentView];
                NSLayoutConstraint *width = [contentView.widthAnchor constraintEqualToAnchor:previousContentView.widthAnchor];
                width.priority = 250;
                width.active = true;
             else 
                [containerView.leadingAnchor constraintEqualToAnchor:contentView.leadingAnchor].active = true;
            

            [contentView.topAnchor constraintEqualToAnchor:scrollView.topAnchor].active = true;
            [contentView.bottomAnchor constraintEqualToAnchor:scrollView.bottomAnchor].active = true;
            [contentView.leadingAnchor constraintEqualToAnchor:scrollView.leadingAnchor].active = true;
            [contentView.trailingAnchor constraintEqualToAnchor:scrollView.trailingAnchor].active = true;

            [scrollView.topAnchor constraintEqualToAnchor:self.imageView.topAnchor].active = true;
            [scrollView.bottomAnchor constraintEqualToAnchor:self.imageView.bottomAnchor].active = true;
            [scrollView.leadingAnchor constraintEqualToAnchor:self.imageView.leadingAnchor].active = true;
            [scrollView.trailingAnchor constraintEqualToAnchor:self.imageView.trailingAnchor].active = true;

            previousContentView = contentView;
        
        [containerView.trailingAnchor constraintEqualToAnchor:previousContentView.trailingAnchor].active = true;

- (UIView *)addRandomColoredView

    UIView *someView = [[UIView alloc] init];
    someView.translatesAutoresizingMaskIntoConstraints = false;
    [someView setBackgroundColor:[UIColor blackColor]];
    return someView;


-(UIScrollView *)addRandomScrollView

    UIScrollView *scrollView = [[UIScrollView alloc] init];
    [scrollView setDelegate:self];
    [scrollView setTranslatesAutoresizingMaskIntoConstraints:false];
    [scrollView setBackgroundColor:[UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0]];
    [scrollView setMaximumZoomScale:2.5f];
    [scrollView setMinimumZoomScale:0.5f];
    [scrollView setZoomScale:scrollView.minimumZoomScale];
    return scrollView;


#pragma mark - UIScrollViewDelegate
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView

    return self.imageView;

我相信 scrollView 得到了它的contentSize,因为它正在显示图像并且我可以滚动。为什么我不能放大或缩小?

【问题讨论】:

【参考方案1】:

你必须为你的滚动视图指定一个代理,然后实现viewForZoomingInScrollView来告诉它缩放图像视图:

- (UIView *)addScrollViewWithImageView 
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.translatesAutoresizingMaskIntoConstraints = false;
    scrollView.maximumZoomScale = 200.0;
    scrollView.minimumZoomScale = 0.1;
    scrollView.delegate = self;

    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"test.png"]];
    imageView.translatesAutoresizingMaskIntoConstraints = false;

    [self.view addSubview:scrollView];

    [scrollView addSubview:imageView];

    [NSLayoutConstraint activateConstraints:@[
        [imageView.topAnchor    constraintEqualToAnchor:scrollView.topAnchor],
        [imageView.leftAnchor   constraintEqualToAnchor:scrollView.leftAnchor],
        [imageView.rightAnchor  constraintEqualToAnchor:scrollView.rightAnchor],
        [imageView.bottomAnchor constraintEqualToAnchor:scrollView.bottomAnchor]
    ]];

    return scrollView;


- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView 
    return scrollView.subviews.firstObject;

然后用图像视图填充这些可缩放的滚动视图,您的viewDidLoad 可能如下所示:

- (void)viewDidLoad 
    [super viewDidLoad];

    UIView *previousContentView = nil;

    for (NSInteger i = 0; i < 3; i++) 
        UIView *contentView = [self addScrollViewWithImageView];
        [self.view.leadingAnchor constraintEqualToAnchor:contentView.leadingAnchor].active = true;
        [self.view.trailingAnchor constraintEqualToAnchor:contentView.trailingAnchor].active = true;
        if (previousContentView) 
            [HorizontalSeparatorView addSeparatorBetweenView:previousContentView secondView:contentView];
            NSLayoutConstraint *height = [contentView.heightAnchor constraintEqualToAnchor:previousContentView.heightAnchor];
            height.priority = 250;
            height.active = true;
         else 
            [self.view.topAnchor constraintEqualToAnchor:contentView.topAnchor].active = true;
        
        previousContentView = contentView;
    
    [self.view.bottomAnchor constraintEqualToAnchor:previousContentView.bottomAnchor].active = true;

【讨论】:

感谢您的回答罗。它对我帮助很大。我现在的一切都按照我想要的方式工作。使用自动布局缩小时,您能指导我将滚动视图内容居中吗?这可能吗?我尝试将图像视图的宽度、高度、centerXAnchor、centerYAnchor 分别连接到滚动视图的宽度、高度、centerXAnchor、centerYAnchor。这仅有助于在未缩放时将图像视图居中。【参考方案2】:

试试 MWPhotoBrowser。它内置了滚动和缩放功能。你会得到比你需要的更多的东西。祝你好运。

【讨论】:

Apple 提供了示例代码 PhotoScroller 以满足您的需求。我正在与您分享。 developer.apple.com/library/ios/samplecode/PhotoScroller/…

以上是关于iOS - UIScrollView 可以滚动但不能缩放的主要内容,如果未能解决你的问题,请参考以下文章

UIScrollView在iOS11中不滚动

ios 将 UIScrollView 滚动条颜色更改为不同颜色

UIScrollview在垂直和水平ios中滚动

iOS --UIScrollView的学习

iOS UIScrollView的使用

UIScrollView 如何改变框架,但不改变边界? UIScrollView 是如何工作的?