如何全屏显示图像的滚动视图? [英] 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?
这是全屏显示 UIImageView 的代码.它正在工作.
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];
}
这是我的图片滚动视图
//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);
}
推荐答案
这个怎么样,添加一个选中的图片属性:
How about this, add a selected image property:
@property (strong, nonatomic) UIImageView *selected;
给滚动视图中的每个 imageView 一个 tap gr:
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];
}
修改post方法获取一个图片视图参数:
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 可以通过选中的图片视图:
Unzoom tap can pass the selected image view:
- (void)unzoom:(UIGestureRecognizer *)gr {
[self setImageView:self.selected zoomed:NO animated:YES];
}
这篇关于如何全屏显示图像的滚动视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!