マルチタップイベントを検知し、シングルタップした時と、素早くダブルタップした時の処理を分けるサンプルを以下に作成しました。
下記よりサンプルのダウンロードが行えます。
ダウンロード
ソースコード
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
| #import "ViewController.h"
@interface ViewController ()
@property (nonatomic, assign) BOOL singleTapReady;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
//画面がタッチされたらシングルタップフラグを解除
_singleTapReady = NO;
}
- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
NSInteger tapCount = [[touches anyObject] tapCount];
if (tapCount < 2) {
//tapCountが 2より小さければシングルタップフラグを立てる
_singleTapReady = YES;
//singleTap確定メソッドを0.3秒後に遅延実行
[self performSelector:@selector(singleTap:)
withObject:nil
afterDelay:0.3f];
} else {
//dabouleTap確定メソッドを実行
[self performSelector:@selector(doubleTap)];
}
}
- (void)singleTap:(id)selector
{
//他の touchesBeganが呼ばれていたらキャンセル
if (!_singleTapReady) {
return;
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
message:@"Single Tap!"
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:@"OK", nil];
[alert show];
}
- (void)doubleTap
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
message:@"Double Tap!!"
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:@"OK", nil];
[alert show];
}
@end
|
お薦めの参考書
Cocos2d-xスマートフォン2Dゲーム開発講座 Cocos2d-x 3対応
Cocos2d-xを利用したスマートフォン向け3Dゲーム開発の手法を、サンプルを基に作りながら学ぶ事ができます。実際に遊べるゲームサンプルが4種収録されており、iOS / Android両対応のゲームを開発したい方には必見の一冊になっています。