iOS - UIScrollView可以滚动但无法缩放

时间:2016-03-06 14:49:41

标签: ios objective-c uiscrollview

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

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

UIImageView

代码:

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

我相信scrollView正在获取它的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; } 因为它正在显示图像而我可以滚动。为什么我无法放大或缩小?

2 个答案:

答案 0 :(得分:1)

您必须为滚动视图指定一个委托,然后实施[SetUp] public void SetUp() { // setup only for cases where obj.Number == 1 _service .Setup(x => x.ProcessInt(It.Is<MyObj>(o => o.Number == 1 ))) .Returns(1); // setup only for cases where obj.Number == 2 _service .Setup(x => x.ProcessInt(It.Is<MyObj>(o => o.Number == 2 ))) .Returns(2); } 告诉它缩放图像视图:

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

答案 1 :(得分:0)

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