UIScrollView + UIImageView放大/缩小
我有 UIScrollView 包含 3*UIImageView,每个 UIImageView 都有一个图像。 我使用的代码:
scrollView.delegate = self;
[self.scrollView setBackgroundColor:[UIColor blackColor]];
[scrollView setCanCancelContentTouches:NO];
scrollView.maximumZoomScale = 4.0;
scrollView.minimumZoomScale = 0.75;
scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView.clipsToBounds = YES;
scrollView.scrollEnabled = YES;
scrollView.pagingEnabled = YES;
NSUInteger nimages = 0;
CGFloat cx = 0;
for (; ; nimages++) {
NSString *imageName = [NSString stringWithFormat:@"%d.jpg", (nimages + 1)];
UIImage *image = [UIImage imageNamed:imageName];
if (image == nil) {
break;
}
imageView = [[UIImageView alloc] initWithImage:image];
CGRect rect = imageView.frame;
rect.size.height = image.size.height;
rect.size.width = image.size.width;
rect.origin.x = ((scrollView.frame.size.width - image.size.width) / 2) + cx;
rect.origin.y = ((scrollView.frame.size.height - image.size.height) / 2);
[imageView setFrame:CGRectMake(cx, 0, self.view.frame.size.width, self.view.frame.size.height)];
[scrollView addSubview:imageView];
[imageView release];
cx += scrollView.frame.size.width;
}
[scrollView setContentSize:CGSizeMake(cx, [scrollView bounds].size.height)];
如何使用多点触控使 ScrollView 可缩放? 我使用了这段代码:
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return imageView;
}
但它无法正常工作,图像重叠,因为 ScrollView 包含多个图像。
我该如何修复它? 谢谢
I have UIScrollView contains 3*UIImageView, each UIImageView has an image.
Code I used:
scrollView.delegate = self;
[self.scrollView setBackgroundColor:[UIColor blackColor]];
[scrollView setCanCancelContentTouches:NO];
scrollView.maximumZoomScale = 4.0;
scrollView.minimumZoomScale = 0.75;
scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView.clipsToBounds = YES;
scrollView.scrollEnabled = YES;
scrollView.pagingEnabled = YES;
NSUInteger nimages = 0;
CGFloat cx = 0;
for (; ; nimages++) {
NSString *imageName = [NSString stringWithFormat:@"%d.jpg", (nimages + 1)];
UIImage *image = [UIImage imageNamed:imageName];
if (image == nil) {
break;
}
imageView = [[UIImageView alloc] initWithImage:image];
CGRect rect = imageView.frame;
rect.size.height = image.size.height;
rect.size.width = image.size.width;
rect.origin.x = ((scrollView.frame.size.width - image.size.width) / 2) + cx;
rect.origin.y = ((scrollView.frame.size.height - image.size.height) / 2);
[imageView setFrame:CGRectMake(cx, 0, self.view.frame.size.width, self.view.frame.size.height)];
[scrollView addSubview:imageView];
[imageView release];
cx += scrollView.frame.size.width;
}
[scrollView setContentSize:CGSizeMake(cx, [scrollView bounds].size.height)];
How can I make ScrollView zoom-able using multi-touch?
I used this code:
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return imageView;
}
But it didn't work correctly, images got overlapped, because ScrollView contains multiple images.
How can I fix it?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不要直接将图像视图添加到滚动视图中,而是这样做:
为每个 UIImageView 单独创建一个新的 UIScrollView 对象,并将框架设置为与图像视图相同。将图像视图作为子视图添加到滚动视图中。请记住,这些滚动视图对象仅用于缩放图像,因此请设置zoomScale、minimumZoomScale 和maximumZoomScale,并设置滚动委托。在委托控制器中:
然后,将所有滚动视图对象添加到您在代码中使用的滚动视图。该滚动视图仅用于滚动上面创建的子滚动视图。
明白了吗?
Don't add the image views to the scrollview directly, instead, do it this way:
Make a new UIScrollView object for each UIImageView separately and set the frame to be the same with the image view. Add the image view to the scrollview as a subview. Remember, these scroll view objects are used to zoom the images only, so set the zoomScale, minimumZoomScale and maximumZoomScale, also set the scroll delegate. In the delegate controller:
Then, add all the scroll view objects to the scroll view you used in your code. This scroll view is used only to scroll the sub scrollviews created above.
Got the idea?
您可以在图像视图中使用 PinchGesture...
不需要使用 3 个滚动视图。
You can use PinchGesture to your image views...
There is no need of 3 scrollviews to be used.
尝试对您的 3 个图像进行此操作:
.m 控制器:
此代码对我有用:)
try this for your 3 images:
.m Controller:
This code worked for me :)