【发布时间】:2014-05-06 04:43:11
【问题描述】:
我正在尝试了解手势识别器的工作原理,并尝试让手势识别器告诉我长触摸何时开始以及何时结束,即使触摸没有移动。
在viewDidLoad 中,我添加了一个名为游戏的子视图。
在游戏中,我实现了两种识别触摸的方法。一个工作,但不做我想要的。另一个不起作用。
Method1我只是添加了两个方法:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"Began");
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *) event {
NSLog(@"Ended");
}
这行得通,无需实例化手势识别器。谁能告诉我为什么?
这个问题是- (void)touchedEnded: 仅在触摸移动时才被调用,而不是在触摸在它开始的同一位置结束时才被调用。因此,如果我触摸并移动并放手,这两个函数都会被调用。如果我触摸并按住并放开(不动),则只会调用 - (void)touchesBegan。
方法 2 我实例化了一个手势识别器:
@property (nonatomic, strong) UILongPressGestureRecognizer *lprg;
然后在我的设置中:
self.lprg = [UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
然后:
- (void)handleLongPress:(UILongPressGestureRecognizer *)recognizer {
NSLog(@"Handling");
switch (recognizer.state) {
case UIGestureRecognizerStateBegan:
// ...
}
}
但是这个,我以编程方式实例化识别器,没有工作。我从来没有在控制台中得到任何 NSLog 输出。
【问题讨论】: