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

时间:2014-05-28 15:18:48

标签: ios objective-c uiscrollview uiimageview

我实现了一种方法来显示黑色背景的全屏图像。我想将它改编成图像的滚动视图。用户应该能够在全屏模式下滚动并转到下一个图像。我怎么能这样做?

这是在全屏显示UIImageView的代码。它正在发挥作用。

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

这是我的图像滚动视图

  //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);

}

2 个答案:

答案 0 :(得分:1)

我猜你可以使用UICollectionView,它会更有效。

答案 1 :(得分:1)

怎么样,添加一个选定的图像属性:

@property (strong, nonatomic) UIImageView *selected;

在滚动视图中为每个imageView添加一个tap gr:

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

点击缩放只是这样做:

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

修改已发布的方法以获取图像视图参数:

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

取消缩放点按可以传递所选的图片视图:

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