我试图找出如何在游戏即时制作中检测触摸和保持屏幕方法。我使用触摸开始单击(让角色向上移动)当他们触摸并保持按住时,我希望角色直接向前移动。
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
//code to make him move up
}
有关如何检测触摸和保持的任何想法?
答案 0 :(得分:2)
Objective-c
// Add guesture recognizer
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(buttonDidLongPress:)];
[self.button addGestureRecognizer:longPress];
// Call back event
- (void)buttonDidLongPress:(UILongPressGestureRecognizer*)gesture
{
switch (gesture.state) {
case UIGestureRecognizerStateBegan:
{
// Code
}
break;
case UIGestureRecognizerStateEnded:
{
//Code
}
break;
default:
break;
}
}
<强>夫特强>
// Add guesture recognizer
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(longPress(_:)))
self.button.addGestureRecognizer(longPress)
// Call back event
func longPress(guesture: UILongPressGestureRecognizer) {
switch guesture.state {
case UIGestureRecognizerState.began:
//Code
break
case UIGestureRecognizerState.ended:
//Code
break
default:
break
}
}
别忘了用UIGestureRecognizerDelegate扩展课程
答案 1 :(得分:0)
尝试:
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
[self.view addGestureRecognizer:longPress];
-(void) handleLongPress: (UIGestureRecognizer *)longPress {
switch (longPress.state) {
case UIGestureRecognizerStateBegan:
// Called when long press for minimum time duration
break;
case UIGestureRecognizerStateChanged:
// Long pressed and dragged
break;
case UIGestureRecognizerStateRecognized:
// Successfully recognised Long touch
break;
default:
break;
}
if (longPress.state==UIGestureRecognizerStateEnded) {
NSLog(@"LONG PRESSED");
}
}