简体   繁体   English

如何全屏显示图像的滚动视图?

[英]How to display a scrollview of images in fullscreen?

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. 这是用于全屏显示UIImageView的代码。 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);

} }

我猜您可以使用UICollectionView,它将更加有效。

How about this, add a selected image property: 怎么样,添加一个选定的图像属性:

@property (strong, nonatomic) UIImageView *selected;

Give each imageView in the scrollview a tap gr: 给滚动视图中的每个imageView轻按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];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM