如何在iOS中点击缩放和双击缩小

如何在iOS中点击缩放和双击缩小

本文介绍了如何在iOS中点击缩放和双击缩小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 UIScrollView 开发一个显示 UIImages 库的应用程序,我的问题是,如何点击缩放并双击 zoom out,使用 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 - 在你的viewController中

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中点击缩放和双击缩小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 09:18