我有一个CCSprite,我想用手势移动。问题是我对Cocos2D完全不熟悉。我希望我的精灵在手势开始时执行一个动作,在手势停止时执行另一个动作,在手势正确时执行另一个动作,对左手执行相同操作。有人能指出我正确的方向吗?
谢谢!
答案 0 :(得分:57)
显然,每个UISwipeGestureRecognizer都只能检测给定方向的滑动。即使方向标志可以被“或”在一起,UISwipeGestureRecognizer也会忽略其他标志。
解决方案是为您想要识别滑动手势的每个方向添加一个UISwipeGestureRecognizer,并相应地将每个识别器的方向设置为向上,向下,向左和向右。如果您想测试任何方向的滑动,则必须添加四个UISwipeGestureRecognizer。
这有点奇怪,但这是它对我有用的唯一方式。
答案 1 :(得分:27)
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)];
swipeGesture.direction = UISwipeGestureRecognizerDirectionUp|UISwipeGestureRecognizerDirectionDown;
[self.gestureAreaView addGestureRecognizer:swipeGesture];
[swipeGesture release];
-(void)handleSwipeGesture:(UISwipeGestureRecognizer *) sender
{
//Gesture detect - swipe up/down , can't be recognized direction
}
or
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)];
swipeGesture.direction = UISwipeGestureRecognizerDirectionUp;
[self.view addGestureRecognizer:swipeGesture];
[swipeGesture release];
UISwipeGestureRecognizer *swipeGesture2 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)];
swipeGesture2.direction = UISwipeGestureRecognizerDirectionDown;
[self.view addGestureRecognizer:swipeGesture2];
[swipeGesture2 release];
-(void)handleSwipeGesture:(UISwipeGestureRecognizer *) sender
{
//Gesture detect - swipe up/down , can be recognized direction
if(sender.direction == UISwipeGestureRecognizerDirectionUp)
{
}
else if(sender.direction == UISwipeGestureRecognizerDirectionDown)
{
}
}
答案 2 :(得分:8)
使用UIPanGestureRecogizer并检测您关心的滑动方向。有关详细信息,请参阅UIPanGestureRecognizer文档。 -rrh
// add pan recognizer to the view when initialized
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panRecognized:)];
[panRecognizer setDelegate:self];
[self addGestureRecognizer:panRecognizer]; // add to the view you want to detect swipe on
-(void)panRecognized:(UIPanGestureRecognizer *)sender
{
if (sender.state == UIGestureRecognizerStateBegan) {
// you might want to do something at the start of the pan
}
CGPoint distance = [sender translationInView:self]; // get distance of pan/swipe in the view in which the gesture recognizer was added
CGPoint velocity = [sender velocityInView:self]; // get velocity of pan/swipe in the view in which the gesture recognizer was added
float usersSwipeSpeed = abs(velocity.x); // use this if you need to move an object at a speed that matches the users swipe speed
NSLog(@"swipe speed:%f", usersSwipeSpeed);
if (sender.state == UIGestureRecognizerStateEnded) {
[sender cancelsTouchesInView]; // you may or may not need this - check documentation if unsure
if (distance.x > 0) { // right
NSLog(@"user swiped right");
} else if (distance.x < 0) { //left
NSLog(@"user swiped left");
}
if (distance.y > 0) { // down
NSLog(@"user swiped down");
} else if (distance.y < 0) { //up
NSLog(@"user swiped up");
}
// Note: if you don't want both axis directions to be triggered (i.e. up and right) you can add a tolerence instead of checking the distance against 0 you could check for greater and less than 50 or 100, etc.
}
}
答案 3 :(得分:7)
defaut方向是UISwipeGestureRecognizerDirectionRight。多个方向也可以这样指定:
[swipeGesture setDirection: UISwipeGestureRecognizerDirectionRight|UISwipeGestureRecognizerDirectionLeft];
/// 但如果你想得到每一个方向,那就是:
UISwipeGestureRecognizer *swipeGestureR = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGestureRight:)];
[swipeGestureR setDirection: UISwipeGestureRecognizerDirectionRight ];
[[[CCDirector sharedDirector] openGLView] addGestureRecognizer:swipeGestureR];
[swipeGestureR release];
UISwipeGestureRecognizer *swipeGestureL = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGestureLeft:)];
[swipeGestureL setDirection: UISwipeGestureRecognizerDirectionLeft];
[[[CCDirector sharedDirector] openGLView] addGestureRecognizer:swipeGestureL];
[swipeGestureL release];
向左滑动时将调用函数handleSwipeGestureLeft,向右滑动时将调用handleSwipeGestureRight
答案 4 :(得分:1)
为每个斧头(水平和垂直)添加一个UISwipeGestureRecognizer:
UISwipeGestureRecognizer *horizontalSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(action)];
[horizontalSwipe setDirection:(UISwipeGestureRecognizerDirectionRight |
UISwipeGestureRecognizerDirectionLeft )];
[self.view addGestureRecognizer:horizontalSwipe];
UISwipeGestureRecognizer *verticalSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(action)];
[verticalSwipe setDirection:(UISwipeGestureRecognizerDirectionUp |
UISwipeGestureRecognizerDirectionDown )];
[self.view addGestureRecognizer:verticalSwipe];
答案 5 :(得分:0)
-(void)addGesture {
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeGesture:)];
[self.view addGestureRecognizer:swipeGesture];
[swipeGesture release];
}
-(void)handleSwipeGesture:(UISwipeGestureRecognizer *) sender {
if (sender.direction == UISwipeGestureRecognizerDirectionUp) {
//do something
}
else if (sender.direction == UISwipeGestureRecognizerDirectionDown) {
//do something
}
else if (sender.direction == UISwipeGestureRecognizerDirectionLeft) {
//do something
}
else if (sender.direction == UISwipeGestureRecognizerDirectionRight) {
//do something
}
}
也可以使用开关代替所有if语句
答案 6 :(得分:0)
即使这里有很多好消息,我也找不到能够快速回答的答案。
如果您要区分滑动是left
还是right
还是up
还是down
,您需要创建一个 new 每个方向的em> UISwipeGestureRecognizer
。
然而!这并不是很糟糕,因为您可以将每个手势识别器路由到同一个选择器,然后可以按照您的预期使用switch语句。
首先,为每个方向添加手势识别器并将它们路由到同一个选择器:
- (void)setupSwipeGestureRecognizers
{
UISwipeGestureRecognizer *rightSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(userDidSwipeScreen:)];
rightSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
UISwipeGestureRecognizer *leftSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(userDidSwipeScreen:)];
leftSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:rightSwipeGestureRecognizer];
[self.view addGestureRecognizer:leftSwipeGestureRecognizer];
}
第二,使用switch语句区分路线:
- (void)userDidSwipeScreen:(UISwipeGestureRecognizer *)swipeGestureRecognizer
{
switch (swipeGestureRecognizer.direction) {
case UISwipeGestureRecognizerDirectionLeft: {
// Handle left
break;
}
case UISwipeGestureRecognizerDirectionRight: {
// Handle right
break;
}
default: {
break;
}
}
}