How to tap to zoom and double tap to zoom out in iOS?(如何在iOS中点击放大和双击缩小?)
问题描述
我正在开发一个应用程序以使用 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中点击放大和双击缩小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在iOS中点击放大和双击缩小?
基础教程推荐
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01