ios 在scrollView内的UIImageView上捏缩放效果?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16877807/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pinch To Zoom Effect on UIImageView inside scrollView?
提问by KingPolygon
I'm using storyboard (iOS 6.0) to create a photo gallery viewer for my app. This is how my imageViewController is set up in storyboard:
我正在使用故事板 (iOS 6.0) 为我的应用程序创建照片库查看器。这是我的 imageViewController 在故事板中的设置方式:
I've made sure to enable userInteraction and multiple touches on both the imageView and scrollView. What I want to do is, on pinch I want to zoom into the imageView (maximum scale 3) and be able to pan around. This is what I currently have, however, even though the pinch gesture is detected the scale does not change.
我确保在 imageView 和 scrollView 上启用 userInteraction 和多次触摸。我想要做的是,在捏我想放大图像视图(最大比例 3)并能够平移。然而,这就是我目前所拥有的,即使检测到捏合手势,比例也不会改变。
- (IBAction)imagePinched:(id)sender {
if (pinchRecognizer.state == UIGestureRecognizerStateEnded || pinchRecognizer.state == UIGestureRecognizerStateChanged) {
NSLog(@"gesture.scale = %f", pinchRecognizer.scale);
CGFloat currentScale = self.fullScreenView.frame.size.width / self.fullScreenView.bounds.size.width;
CGFloat newScale = currentScale * pinchRecognizer.scale;
if (newScale < 1) {
newScale = 1;
}
if (newScale > 3) {
newScale = 3;
}
CGAffineTransform transform = CGAffineTransformMakeScale(newScale, newScale);
self.fullScreenView.transform = transform;
pinchRecognizer.scale = 1;
}
}
Most questions and tutorials online deal with programmatically creating the views and doing this, but the less code the better (in my eyes). What's the best way to get this to work with storyboard? Thank you in advance!!!
大多数在线问题和教程都涉及以编程方式创建视图并执行此操作,但代码越少越好(在我看来)。让它与故事板一起工作的最佳方法是什么?先感谢您!!!
UPDATED:
更新:
Here is my full .m file code:
这是我的完整 .m 文件代码:
- (void)viewDidLoad
{
[super viewDidLoad];
//Assign an image to this controller's imageView
fullScreenView.image = [UIImage imageNamed:imageString];
//Allows single and double tap to work
[singleTapRecognizer requireGestureRecognizerToFail: doubleTapRecognizer];
}
- (IBAction)imageTapped:(id)sender {
NSLog(@"Image Tapped.");
//On tap, fade out viewController like the twitter.app
[self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)imageDoubleTapped:(id)sender {
NSLog(@"Image Double Tapped.");
//On double tap zoom into imageView to fill in the screen.
[fullScreenView setContentMode:UIViewContentModeScaleAspectFill];
}
- (IBAction)imagePinched:(id)sender {
if (pinchRecognizer.state == UIGestureRecognizerStateEnded || pinchRecognizer.state == UIGestureRecognizerStateChanged) {
NSLog(@"gesture.scale = %f", pinchRecognizer.scale);
CGFloat currentScale = self.fullScreenView.frame.size.width / self.fullScreenView.bounds.size.width;
CGFloat newScale = currentScale * pinchRecognizer.scale;
if (newScale < 1) {
newScale = 1;
}
if (newScale > 3) {
newScale = 3;
}
CGAffineTransform transform = CGAffineTransformMakeScale(newScale, newScale);
self.fullScreenView.transform = transform;
pinchRecognizer.scale = 1;
}
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.fullScreenView;
}
-(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale {
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
回答by Vineesh TP
I think better solution in Apple Documentation
我认为 Apple 文档中有更好的解决方案
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.imageView;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.scrollView.minimumZoomScale=0.5;
self.scrollView.maximumZoomScale=6.0;
self.scrollView.contentSize=CGSizeMake(1280, 960);
self.scrollView.delegate=self;
}
回答by ceekay
The first step is to make sure your views have the correct delegates implemented. For example in the .m file
第一步是确保您的视图实现了正确的委托。例如在 .m 文件中
@interface myRootViewController () <.., UIGestureRecognizerDelegate, UIScrollViewDelegate, ...>
From the documentation, make sure you have this implemented:
从文档中,确保您已实现此功能:
The UIScrollView class can have a delegate that must adopt the UIScrollViewDelegate protocol. For zooming and panning to work, the delegate must implement both viewForZoomingInScrollView:and scrollViewDidEndZooming:withView:atScale:; in addition, the maximum (maximumZoomScale) and minimum ( minimumZoomScale) zoom scale must be different.
UIScrollView 类可以有一个必须采用 UIScrollViewDelegate 协议的委托。要使缩放和平移工作,委托必须同时实现viewForZoomingInScrollView:和scrollViewDidEndZooming:withView:atScale:;此外,最大(maximumZoomScale)和最小(minimumZoomScale)缩放比例必须不同。
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.fullScreenView;
}
-(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale
{}
The scrollViewDidEndZooming method can stay empty for now. If you already did that or it still doesnt work, please post more of your code and then it is easier to help more specifically.
scrollViewDidEndZooming 方法现在可以保持为空。如果您已经这样做了或者它仍然不起作用,请发布更多您的代码,然后更容易提供更具体的帮助。
回答by Nate
You need to do what Wadi suggested above but also set the setMinimumZoomScale to be different from setMaximumZoomScale. They are both 1.0f by default.
您需要按照 Wadi 上面的建议进行操作,但还要将 setMinimumZoomScale 设置为与 setMaximumZoomScale 不同。默认情况下,它们都是 1.0f。
After that, the UIImageView
should be pinchable
之后,UIImageView
应该是可捏的
回答by lucaslt89
Hereis how Apple recomends to do what you want to do. I used the code provided by Apple and it worked like a charm! If you want to optimize memory management, you can use iCarousel(made by nicklockwood), which have a cache management for dealloc unused views!
以下是 Apple 建议您做您想做的事情的方式。我使用了 Apple 提供的代码,它的效果非常棒!如果你想优化内存管理,你可以使用iCarousel(由 nicklockwood 制作),它有一个用于释放未使用视图的缓存管理!
回答by Delete
I have created a fully working demo application (similar in nature to Facebook's default photo gallery) which demonstrates pinch to zoom of Image View nested inside a scroll view with AutoLayout and storyboards. View my project here: http://rexstjohn.com/facebook-like-ios-photo-modal-gallery-swipe-gestures/.
我创建了一个完整的演示应用程序(本质上类似于 Facebook 的默认照片库),它演示了缩放嵌套在带有 AutoLayout 和故事板的滚动视图中的图像视图。在此处查看我的项目:http: //rexstjohn.com/facebook-like-ios-photo-modal-gallery-swipe-gestures/。
It also includes async photo loading via MKNetworkKit and gesture based swiping through a photo gallery. Enjoy and I hope it saves everyone time trying to figure this out because it is somewhat annoying.
它还包括通过 MKNetworkKit 的异步照片加载和基于手势的在照片库中滑动。享受,我希望它可以节省每个人尝试解决这个问题的时间,因为它有点烦人。
回答by Thomas Maw
I've created a class to handle zooming of UIImageViews similar to Instagram. Might be what you're looking for, otherwise you can look at how I achieved it. https://github.com/twomedia/TMImageZoom
我创建了一个类来处理类似于 Instagram 的 UIImageViews 缩放。可能是你正在寻找的,否则你可以看看我是如何实现的。https://github.com/twomedia/TMImageZoom