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

Bru*_*uno 21 objective-c uiscrollview ios uitapgesturerecognizer

我正在开发一个应用程序来显示一个UIImages使用a 的画廊UIScrollView,我的问题是,如何点击zoom并双击zoom,如何处理时它是如何工作的UIScrollView.

Gaz*_*dge 39

您需要实现UITapGestureRecognizer -文档在这里 -在你的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];

}
Run Code Online (Sandbox Code Playgroud)

基本上这段代码表示当UITapGestureself.view方法中注册a时,tapOncetapTwice将被调用,self具体取决于它是单击还是双击.因此,您需要将以下方法添加到您的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];
}
Run Code Online (Sandbox Code Playgroud)

希望有所帮助

  • [tapOnce requireGestureRecognizerToFail:tapTwice]; 正是我想要的.谢谢! (2认同)