如何在 iOS 中点击放大和双击缩小?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9008975/
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
How to tap to zoom and double tap to zoom out in iOS?
提问by Bruno
I'm developing an application to display a gallery of UIImages
by using a UIScrollView
, my question is, how to tap to zoom
and double tap to zoom
out, how does it work when handling with UIScrollView
.
我开发一个应用程序来显示的画廊UIImages
使用UIScrollView
,我的问题是,如何挖掘到zoom
并双击要zoom
出来,用操作时,它是如何工作的UIScrollView
。
回答by Gaz_Edge
You need to implement UITapGestureRecognizer - docs here- in your viewController
您需要在您的 viewController 中实现 UITapGestureRecognizer -此处的文档
- (void)viewDidLoad
{
[super viewDidLoad];
// what object is going to handle the gesture when it gets recognised ?
// the argument for tap is the gesture that caused this message to be sent
UITapGestureRecognizer *tapOnce = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOnce:)];
UITapGestureRecognizer *tapTwice = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTwice:)];
// set number of taps required
tapOnce.numberOfTapsRequired = 1;
tapTwice.numberOfTapsRequired = 2;
// stops tapOnce from overriding tapTwice
[tapOnce requireGestureRecognizerToFail:tapTwice];
// now add the gesture recogniser to a view
// this will be the view that recognises the gesture
[self.view addGestureRecognizer:tapOnce];
[self.view addGestureRecognizer:tapTwice];
}
Basically this code is saying that when a UITapGesture
is registered in self.view
the method tapOnceor tapTwicewill be called in self
depending on if its a single or double tap. You therefore need to add these tap methods to your UIViewController
:
基本上这段代码是说,当 aUITapGesture
在self.view
方法中注册时,tapOnce或tapTwice将被调用,self
具体取决于它是单击还是双击。因此,您需要将这些 tap 方法添加到您的UIViewController
:
- (void)tapOnce:(UIGestureRecognizer *)gesture
{
//on a single tap, call zoomToRect in UIScrollView
[self.myScrollView zoomToRect:rectToZoomInTo animated:NO];
}
- (void)tapTwice:(UIGestureRecognizer *)gesture
{
//on a double tap, call zoomToRect in UIScrollView
[self.myScrollView zoomToRect:rectToZoomOutTo animated:NO];
}
Hope that helps
希望有帮助
回答by Avt
Swift 3.0version that zooms twice on double tap.
双击可放大两次的Swift 3.0版本。
@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var imageView: UIImageView!
Somewhere (usually in viewDidLoad):
某处(通常在 viewDidLoad 中):
let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(onDoubleTap(gestureRecognizer:)))
tapRecognizer.numberOfTapsRequired = 2
scrollView.addGestureRecognizer(tapRecognizer)
Handler:
处理程序:
func onDoubleTap(gestureRecognizer: UITapGestureRecognizer) {
let scale = min(scrollView.zoomScale * 2, scrollView.maximumZoomScale)
if scale != scrollView.zoomScale {
let point = gestureRecognizer.location(in: imageView)
let scrollSize = scrollView.frame.size
let size = CGSize(width: scrollSize.width / scale,
height: scrollSize.height / scale)
let origin = CGPoint(x: point.x - size.width / 2,
y: point.y - size.height / 2)
scrollView.zoom(to:CGRect(origin: origin, size: size), animated: true)
print(CGRect(origin: origin, size: size))
}
}