问题描述
我正在开发一个应用程序以使用 UIScrollView
显示 UIImages
库,我的问题是,如何点击到 zoom
并双击以 zoom
缩小,使用 UIScrollView
处理时它是如何工作的.
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
.
推荐答案
你需要实现 UITapGestureRecognizer - docs 这里 - 在你的视图控制器中
You need to implement UITapGestureRecognizer - docs here - in your viewController
- (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];
}
基本上这段代码是说,当 UITapGesture
在 self.view
中注册时,方法 tapOnce 或 tapTwice 将在 self
中调用,具体取决于它是单击还是双击.因此,您需要将这些点击方法添加到您的 UIViewController
:
Basically this code is saying that when a UITapGesture
is registered in self.view
the method tapOnce or tapTwice will be called in self
depending on if its a single or double tap. You therefore need to add these tap methods to your 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];
}
希望有帮助
这篇关于如何在iOS中点击放大和双击缩小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!