How to display a scrollview of images in fullscreen?

假装没事ソ 提交于 2019-12-11 23:22:44

问题


I implemented a method to display a fullscreen image with a black background. I would like to adapt it for a scrollview of images. The user should be able to scroll and go to next images when it is in fullscreen mode. How can I do this?

This is the code to display an UIImageView in fullscreen. It is working.

- (void)setUserPictImageViewZoomed:(BOOL)zoom animated:(BOOL)animated {

UIView *blackView = [self.view viewWithTag:128];
BOOL isZoomed = blackView != nil;

// if our current state (isZoomed) matches the desired state (zoomed), then we're done
if (isZoomed == zoom) return;
NSTimeInterval duration = (animated)? 0.3 : 0.0;

if (zoom) {
    blackView = [[UIView alloc] initWithFrame:self.view.frame];
    blackView.backgroundColor = [UIColor blackColor];
    blackView.tag = 128;
    blackView.alpha = 0.0;
    [self.view addSubview:blackView];

    UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(unzoom:)];
    [blackView addGestureRecognizer:tapGR];

    UIImageView *zoomImageView = [[UIImageView alloc] initWithImage:self.userPictImageView.image];
    zoomImageView.contentMode = UIViewContentModeScaleAspectFit;
    zoomImageView.tag = 129;
    zoomImageView.frame = [self.userPictImageView convertRect:self.userPictImageView.frame toView:blackView];
    [blackView addSubview:zoomImageView];
    [UIView animateWithDuration:duration animations:^{
        zoomImageView.frame = blackView.bounds;
        blackView.alpha = 1.0;
    }];
} else {
    UIImageView *zoomImageView = (UIImageView *)[blackView viewWithTag:129];
    [UIView animateWithDuration:duration animations:^{
        zoomImageView.frame = self.userPictImageView.frame;
        blackView.alpha = 0.0;
    } completion:^(BOOL finished) {
        [blackView removeFromSuperview];
        }];
    }
    }

- (void)unzoom:(UIGestureRecognizer *)gr {
    [self setUserPictImageViewZoomed:NO animated:YES];
}

And this is my scrollview of images

  //PageControl: as many pages as images
int numberOfPicts = [prize.pictures count];

if (numberOfPicts >1)
    [pageControl setNumberOfPages:numberOfPicts];
else
    pageControl.hidden = YES;

//Load images in the scrollview
for (int i = 0; i < numberOfPicts; i++) {
    //Create an imageView object in every 'page' of the scrollView.
    CGRect frame;
    frame.origin.x = self.imagesScrollView.frame.size.width * i;
    frame.origin.y = 0;
    frame.size = self.imagesScrollView.frame.size;

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
    imageView.contentMode = UIViewContentModeScaleAspectFit;

    imageView.image = [prize.pictures objectAtIndex:i];

    [self.imagesScrollView addSubview:imageView];
}

//Set the content size of the scrollview according to the total width of imageView objects.
self.imagesScrollView.contentSize = CGSizeMake(self.imagesScrollView.frame.size.width * numberOfPicts, self.imagesScrollView.frame.size.height);

}


回答1:


I guess you can use UICollectionView, it will be more effective.




回答2:


How about this, add a selected image property:

@property (strong, nonatomic) UIImageView *selected;

Give each imageView in the scrollview a tap gr:

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapToZoom:)];
    [imageView addGestureRecognizer:tap];  // in a loop for each imageView in the scrollview

Tap to zoom just does this:

- (void)taptozoom:(UITapGestureRecognizer *)gr {
    self.selected = (UIImageView *)gr.view;
    [self setImageView:self.selected zoomed:YES animated:YES];
}

Modify the posted method to take an image view parameter:

- (void)setImageView:(UIImageView *)imageView zoomed:(BOOL)zoom animated:(BOOL)animated {

    UIView *blackView = [self.view viewWithTag:128];
    BOOL isZoomed = blackView != nil;

    // if our current state (isZoomed) matches the desired state (zoomed), then we're done
    if (isZoomed == zoom) return;
    NSTimeInterval duration = (animated)? 0.3 : 0.0;
    CGRect unzoomedFrame = [imageView.superview convertRect:imageView.frame toView:blackView];

    if (zoom) {
        blackView = [[UIView alloc] initWithFrame:self.view.frame];
        blackView.backgroundColor = [UIColor blackColor];
        blackView.tag = 128;
        blackView.alpha = 0.0;
        [self.view addSubview:blackView];

        UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(unzoom:)];
        [blackView addGestureRecognizer:tapGR];

        UIImageView *zoomImageView = [[UIImageView alloc] initWithImage:imageView.image];
        zoomImageView.contentMode = UIViewContentModeScaleAspectFit;
        zoomImageView.tag = 129;
        zoomImageView.frame = unzoomedFrame;
        [blackView addSubview:zoomImageView];
        [UIView animateWithDuration:duration animations:^{
            zoomImageView.frame = blackView.bounds;
            blackView.alpha = 1.0;
        }];
    } else {
        UIImageView *zoomImageView = (UIImageView *)[blackView viewWithTag:129];
        [UIView animateWithDuration:duration animations:^{
            zoomImageView.frame = unzoomedFrame;
            blackView.alpha = 0.0;
        } completion:^(BOOL finished) {
            [blackView removeFromSuperview];
        }];
    }
}

Unzoom tap can pass the selected image view:

- (void)unzoom:(UIGestureRecognizer *)gr {
    [self setImageView:self.selected zoomed:NO animated:YES];
}


来源:https://stackoverflow.com/questions/23915621/how-to-display-a-scrollview-of-images-in-fullscreen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!