<span class="lnum" style="color: rgb(96, 96, 96);"> 1: </span><span class="rem" style="color: rgb(0, 128, 0);">//单指单击</span>
2: UITapGestureRecognizer *singleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self
<span class="lnum" style="color: rgb(96, 96, 96);"> 3: </span> action:@selector(handleSingleFingerEvent:)];
4: singleFingerOne.numberOfTouchesRequired = 1; //手指数
<span class="lnum" style="color: rgb(96, 96, 96);"> 5: </span> singleFingerOne.numberOfTapsRequired = 1; <span class="rem" style="color: rgb(0, 128, 0);">//tap次数</span>
6: singleFingerOne.delegate = self;
<span class="lnum" style="color: rgb(96, 96, 96);"> 7: </span>
8: //单指双击
<span class="lnum" style="color: rgb(96, 96, 96);"> 9: </span> UITapGestureRecognizer *singleFingerTwo = [[UITapGestureRecognizer alloc] initWithTarget:self
10: action:@selector(handleSingleFingerEvent:)];
<span class="lnum" style="color: rgb(96, 96, 96);"> 11: </span> singleFingerTwo.numberOfTouchesRequired = 1;
12: singleFingerTwo.numberOfTapsRequired = 2;
<span class="lnum" style="color: rgb(96, 96, 96);"> 13: </span> singleFingerTwo.<span class="kwrd" style="color: rgb(0, 0, 255);">delegate</span> = self;
14:
<span class="lnum" style="color: rgb(96, 96, 96);"> 15: </span> <span class="rem" style="color: rgb(0, 128, 0);">//双指单击</span>
16: UITapGestureRecognizer *doubleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self
<span class="lnum" style="color: rgb(96, 96, 96);"> 17: </span> action:@selector(handleDoubleFingerEvent:)];
18: doubleFingerOne.numberOfTouchesRequired = 2;
<span class="lnum" style="color: rgb(96, 96, 96);"> 19: </span> doubleFingerOne.numberOfTapsRequired = 1;
20: doubleFingerOne.delegate = self;
<span class="lnum" style="color: rgb(96, 96, 96);"> 21: </span>
22: UITapGestureRecognizer *doubleFingerTwo = [[UITapGestureRecognizer alloc] initWithTarget:self
<span class="lnum" style="color: rgb(96, 96, 96);"> 23: </span> action:@selector(handleDoubleFingerEvent:)];
24: doubleFingerTwo.numberOfTouchesRequired = 2;
<span class="lnum" style="color: rgb(96, 96, 96);"> 25: </span> doubleFingerTwo.numberOfTapsRequired = 2;
26: doubleFingerTwo.delegate = self;
<span class="lnum" style="color: rgb(96, 96, 96);"> 27: </span>
28: //如果不加下面的话,当单指双击时,会先调用单指单击中的处理,再调用单指双击中的处理
<span class="lnum" style="color: rgb(96, 96, 96);"> 29: </span> [singleFingerOne requireGestureRecognizerToFail:singleFingerTwo];
30: //同理双指亦是如此
<span class="lnum" style="color: rgb(96, 96, 96);"> 31: </span> [doubleFingerOne requireGestureRecognizerToFail:doubleFingerTwo];
32:
<span class="lnum" style="color: rgb(96, 96, 96);"> 33: </span> [self.view addGestureRecognizer:singleFingerOne];
34: [self.view addGestureRecognizer:singleFingerTwo];
<span class="lnum" style="color: rgb(96, 96, 96);"> 35: </span> [self.view addGestureRecognizer:doubleFingerOne];
36: [self.view addGestureRecognizer:doubleFingerTwo];
<span class="lnum" style="color: rgb(96, 96, 96);"> 37: </span>
38: [singleFingerOne release];
<span class="lnum" style="color: rgb(96, 96, 96);"> 39: </span> [singleFingerTwo release];
40: [doubleFingerOne release];
<span class="lnum" style="color: rgb(96, 96, 96);"> 41: </span> [doubleFingerTwo release];
处理事件的方法,代码:
<span class="lnum" style="color: rgb(96, 96, 96);"> 1: </span><span class="rem" style="color: rgb(0, 128, 0);">//处理单指事件</span>
2: - (void)handleSingleFingerEvent:(UITapGestureRecognizer *)sender
<span class="lnum" style="color: rgb(96, 96, 96);"> 3: </span>{
4: if (sender.numberOfTapsRequired == 1) {
<span class="lnum" style="color: rgb(96, 96, 96);"> 5: </span> <span class="rem" style="color: rgb(0, 128, 0);">//单指单击</span>
6: NSLog(@"单指单击");
<span class="lnum" style="color: rgb(96, 96, 96);"> 7: </span> }<span class="kwrd" style="color: rgb(0, 0, 255);">else</span> <span class="kwrd" style="color: rgb(0, 0, 255);">if</span>(sender.numberOfTapsRequired == 2){
8: //单指双击
<span class="lnum" style="color: rgb(96, 96, 96);"> 9: </span> NSLog(<span class="str" style="color: rgb(0, 96, 128);">@"单指双击"</span>);
10: }
<span class="lnum" style="color: rgb(96, 96, 96);"> 11: </span>}
12: //处理双指事件
<span class="lnum" style="color: rgb(96, 96, 96);"> 13: </span>- (<span class="kwrd" style="color: rgb(0, 0, 255);">void</span>)handleDoubleFingerEvent:(UITapGestureRecognizer *)sender
14: {
<span class="lnum" style="color: rgb(96, 96, 96);"> 15: </span> <span class="kwrd" style="color: rgb(0, 0, 255);">if</span> (sender.numberOfTapsRequired == 1) {
16: //双指单击
<span class="lnum" style="color: rgb(96, 96, 96);"> 17: </span> NSLog(<span class="str" style="color: rgb(0, 96, 128);">@"双指单击"</span>);
18: }else if(sender.numberOfTapsRequired == 2){
<span class="lnum" style="color: rgb(96, 96, 96);"> 19: </span> <span class="rem" style="color: rgb(0, 128, 0);">//双指双击</span>
20: NSLog(@"双指双击");
<span class="lnum" style="color: rgb(96, 96, 96);"> 21: </span> }
22: }
将相应代码复制到你的工程中即可使用,由于代码中已经有详细的解释说明,这里就不在重复解释了。
代码中只是列举了单指与双指对于单击或多击的处理,同理多指的操作需修改numberOfTouchesRequired属性,对点击的次数需修改numberOfTapsRequired属性。
对于其他手势例如UISwipeGestureRecognizer,UILongPressGestureRecognizer,UILongPressGestureRecognizer的操作使用类似处理。