ios objective-c uiscrollview

iOS-UIScrollView puede desplazarse pero no puede hacer zoom



objective-c (2)

Quiero acercar / alejar UIScrollView que contiene un UIImageView . Con mi código a continuación, solo puedo desplazar el contenido de la vista de desplazamiento pero no puedo acercarlo.

La jerarquía de mi vista se ve así:

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

Código:

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

Creo que scrollView obtiene su tamaño de contentSize porque muestra la imagen y puedo desplazarme. ¿Por qué no puedo acercar o alejar?


Pruebe MWPhotoBrowser. Ha incorporado funciones de desplazamiento y zoom. Obtendrás más de lo que necesitas Buena suerte.


viewForZoomingInScrollView especificar un delegado para su vista de desplazamiento, y luego implementar viewForZoomingInScrollView para decirle que viewForZoomingInScrollView la vista de la imagen:

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

Y luego para poblar estas vistas de desplazamiento con zoom con vistas de imagen, su viewDidLoad podría verse así:

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