Ios 如何区分轻敲和按住?
在我的游戏中,我有两种姿势;一次轻击和一次保持。当我将手指放在屏幕上时,它会导致调用Ios 如何区分轻敲和按住?,ios,objective-c,cocos2d-iphone,Ios,Objective C,Cocos2d Iphone,在我的游戏中,我有两种姿势;一次轻击和一次保持。当我将手指放在屏幕上时,它会导致调用touchStart:(UITouch*)touch with event:(UIEvent*)event,同时调用我的点击方法,因此两种手势都会被调用。我怎样才能避免这个问题?我是否必须使用UIGestureRecognizers,或者是否有办法只使用内置的cocos2d方法?我需要这些手势被单独调用,而不是相互结合 typedef NS_ENUM(NSUInteger, BPMovementState) {
touchStart:(UITouch*)touch with event:(UIEvent*)event
,同时调用我的点击方法,因此两种手势都会被调用。我怎样才能避免这个问题?我是否必须使用UIGestureRecognizers
,或者是否有办法只使用内置的cocos2d方法?我需要这些手势被单独调用,而不是相互结合
typedef NS_ENUM(NSUInteger, BPMovementState) {
kTouchUp, //Finger is not on the screen
kTouchDown //Finger is on the screen
};
@implementation HelloWorldScene
{
CCSprite *_hero;
BPMovementState _touchState;
}
- (instancetype)init
self = [super init];
if (!self) return(nil);
// Enable touch handling on scene node
self.userInteractionEnabled = YES;
_touchState = kTouchUp;
return self;
}
- (void)fixedUpdate:(CCTime)delta{
if(_touchState == kTouchDown){
//_hero slide
_hero.position = ccp(_hero.position.x + 1, _hero.position.y);
}
}
-(void) touchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
_touchState = kTouchDown;
if(touch.tapCount == 1)
[_hero jump]
}
- (void)touchEnded:(UITouch *)touch withEvent:(UIEvent *)event{
_touchState = kTouchUp;
}
它可以通过以下方式实现 在标题中定义计时器,即touchTimer 在触摸屏中,开始启动计时器 touchTimer=[NSTimer scheduledTimerWithTimeInterval:延迟目标:自选择器:@selector(touchHasBeenHeld:)用户信息:无重复:否] 如果计时器过期,并且在释放触摸之前调用了选择器-touchbeenheld id。它举行了一次活动。 在这里,您还可以使计时器无效 否则,如果调用了-touchEnded并且计时器仍然有效,则它是一个触摸事件。
再次使计时器无效。如果可以的话,我会继续使用
UIGestureRecognizer
。苹果的手势识别器经过调整,让用户感到熟悉。如果您创建自己的,您的计时或阈值可能会略有不同,并且对于习惯于系统默认设置的用户来说,这些手势会感到奇怪。是一个关于向cocos2d项目添加uigestureRecognitor
的快速教程。您可以记录触摸开始和触摸结束之间的时间。之后,您可以自定义识别点击和按住的标准。点击是友谊的象征。拥抱是爱的象征。