iOS – UIScrollView可以滚动,但不能缩放

我想放大/缩小包含UIImageView UIScrollView 。 与我的代码下面,我只能够滚动滚动视图的内容,但不能放大。

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

 - (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因为它显示图像,我可以滚动。 为什么我无法放大或缩小?

你必须为你的滚动视图指定一个委托,然后实现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; } 

试试MWPhotoBrowser。 它内置了滚动和缩放function。 你会得到超过你的需要。 祝你好运。