UITapGestureRecognizer 手势使用方法

写一个例子吧:

ViewController.h里

#import <UIKit/UIKit.h>


@interface ViewController : UIViewController<UIGestureRecognizerDelegate>


@end


ViewController.m里

#import "ViewController.h"


@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad

{

[super viewDidLoad];

/*单指单击*/

UITapGestureRecognizer *singleFingerOne = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleSingleFingerEvent:)];

singleFingerOne.numberOfTouchesRequired = 1; //手指

singleFingerOne.numberOfTapsRequired = 1; //tap次数

singleFingerOne.delegate = self;

/*单指双击*/

UITapGestureRecognizer *singleFingerTwo = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleSingleFingerEvent:)];

singleFingerTwo.numberOfTouchesRequired = 1;

singleFingerTwo.numberOfTapsRequired = 2;

singleFingerTwo.delegate = self;

/*双指单击*/

UITapGestureRecognizer *doubleFingerOne = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleDoubleFingerEvent:)];

doubleFingerOne.numberOfTouchesRequired = 2;

doubleFingerOne.numberOfTapsRequired = 1;

doubleFingerOne.delegate = self;

UITapGestureRecognizer *doubleFingerTwo = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleDoubleFingerEvent:)];

doubleFingerTwo.numberOfTouchesRequired = 2;

doubleFingerTwo.numberOfTapsRequired = 2;

doubleFingerTwo.delegate = self;

//如果不加下面的话,当单指双击时,会先调用单指单击中的处理,再调用单指双击中的处理

[singleFingerOne requireGestureRecognizerToFail:singleFingerTwo];

//双指也是如此

[doubleFingerOne requireGestureRecognizerToFail:doubleFingerTwo];

[self.view addGestureRecognizer:singleFingerOne];

[self.view addGestureRecognizer:singleFingerTwo];

[self.view addGestureRecognizer:doubleFingerOne];

[self.view addGestureRecognizer:doubleFingerTwo];

[singleFingerOne release];

[singleFingerTwo release];

[doubleFingerOne release];

[doubleFingerTwo release];

}

//处理事件的方法:

-(void)handleSingleFingerEvent:(UITapGestureRecognizer *)sender{

if(sender.numberOfTapsRequired == 1){

NSLog(@"单指单击");

}else if (sender.numberOfTapsRequired == 2){

NSLog(@"单指双击");

}

}

-(void)handleDoubleFingerEvent:(UITapGestureRecognizer *)sender{

if(sender.numberOfTapsRequired == 1){

NSLog(@"双指单击");

}else if (sender.numberOfTapsRequired == 2){

NSLog(@"双指双击");

}


}

- (void)didReceiveMemoryWarning

{

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}


@end



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值