我正在尝试实现将改变单位方向的游戏控制 运动。因此,如果我向右滑动则转向右侧,如果我向下滑动则转向向下方向等。
这是cocos2d游戏,我在CCNode+SFGestureRecognizers
使用UISwipeGestureRecognizer
。
现在我有下一个实现
UISwipeGestureRecognizer *rightSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleRightSwipe:)];
[self addGestureRecognizer:rightSwipeGestureRecognizer];
rightSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
rightSwipeGestureRecognizer.delegate = self;
[rightSwipeGestureRecognizer release];
UISwipeGestureRecognizer *upSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleUpSwipe:)];
[self addGestureRecognizer:upSwipeGestureRecognizer];
upSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionUp;
upSwipeGestureRecognizer.delegate = self;
[upSwipeGestureRecognizer release];
UISwipeGestureRecognizer *leftSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleLeftSwipe:)];
[self addGestureRecognizer:leftSwipeGestureRecognizer];
leftSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
leftSwipeGestureRecognizer.delegate = self;
[leftSwipeGestureRecognizer release];
UISwipeGestureRecognizer *downSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleDownSwipe:)];
[self addGestureRecognizer:downSwipeGestureRecognizer];
downSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionDown;
downSwipeGestureRecognizer.delegate = self;
[downSwipeGestureRecognizer release];
但问题是要识别以下手势,您需要将手指从屏幕上抬起。如果在第一次滑动后没有抬起手指,它将只识别第一次滑动。
现在:
应该如何:
最好的方法是什么?谢谢!
答案 0 :(得分:1)
cancelsTouchesInView应该有所帮助,它可以防止在手势识别器识别出手势时取消触摸事件。这将允许其他手势识别器继续检查他们的手势。
rightSwipeGestureRecognizer.cancelsTouchesInView = NO;
upSwipeGestureRecognizer.cancelsTouchesInView = NO;
leftSwipeGestureRecognizer.cancelsTouchesInView = NO;
downSwipeGestureRecognizer.cancelsTouchesInView = NO;
答案 1 :(得分:1)
好的,有我的解决方案。我不认为这是一个非常好的解决方案,但在我的情况下它是有效的,因为我需要在每个时间间隔获得方向。
所以,在我的CCLayer
子类中:
#define SWIPE_LENGTH 60
#define SWIPE_TANGENT 2
...
@property(nonatomic, assign) CGPoint latestLocation;
...
-(id) init
{
if( (self=[super init]) )
{
self.isTouchEnabled = YES;
}
return self;
}
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
self.latestLocation = location;
}
- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
CGPoint delataLocation = CGPointMake(location.x - self.latestLocation.x, location.y - self.latestLocation.y);
if ( sqrt(pow((delataLocation.x), 2) + pow((delataLocation.y), 2) ) > SWIPE_LENGTH ) {
if (delataLocation.y > 0 && delataLocation.y > SWIPE_TANGENT * ABS(delataLocation.x))
{
[self handleSwipeWithDirestion:SDirectionTypeUp];
} else if (delataLocation.y < 0 && ABS(delataLocation.y) > SWIPE_TANGENT * ABS(delataLocation.x))
{
[self handleSwipeWithDirestion:SDirectionTypeDown];
} else if (delataLocation.x > 0 && delataLocation.x > SWIPE_TANGENT * ABS(delataLocation.y))
{
[self handleSwipeWithDirestion:SDirectionTypeRight];
} else if (delataLocation.x < 0 && ABS(delataLocation.x) > SWIPE_TANGENT * ABS(delataLocation.y))
{
[self handleSwipeWithDirestion:SDirectionTypeLeft];
}
self.latestLocation = location;
}
}