【发布时间】:2014-05-28 15:18:47
【问题描述】:
我实现了一种方法来显示具有黑色背景的全屏图像。我想将其调整为图像的滚动视图。当处于全屏模式时,用户应该能够滚动并转到下一个图像。我该怎么做?
这是全屏显示 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);
}
【问题讨论】:
-
您面临的具体问题是什么?
-
我可以显示单个图像,但它不适用于图像的滚动视图。我正在寻找如何做到这一点的线索。
-
确保你在图片浏览上
setUserInteraction:NO。除此之外,您的代码对我来说看起来不错。
标签: ios objective-c uiscrollview uiimageview