Mag*_*nus 26 iphone xcode cocos2d-iphone uigesturerecognizer
我有一个CCSprite,我想用手势移动.问题是我对Cocos2D完全不熟悉.我希望我的精灵在手势开始时执行一个动作,在手势停止时执行另一个动作,在手势正确时执行另一个动作,对左手执行相同操作.有人能指出我正确的方向吗?
谢谢!
Lea*_*s2D 58
显然,每个UISwipeGestureRecognizer只能检测给定方向的滑动.即使方向标志可以被"或"在一起,UISwipeGestureRecognizer也会忽略其他标志.
解决方案是为您希望识别滑动手势的每个方向添加一个UISwipeGestureRecognizer,并相应地将每个识别器的方向设置为向上,向下,向左和向右.如果要测试任何方向的滑动,则必须添加四个UISwipeGestureRecognizer.
这有点奇怪,但这是它对我有用的唯一方式.
Dev*_*Gom 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)
{
}
}
Run Code Online (Sandbox Code Playgroud)
使用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.
}
}
Run Code Online (Sandbox Code Playgroud)
小智 7
defaut方向是UISwipeGestureRecognizerDirectionRight.多个方向也可以这样指定:
[swipeGesture setDirection: UISwipeGestureRecognizerDirectionRight|UISwipeGestureRecognizerDirectionLeft];
Run Code Online (Sandbox Code Playgroud)
///但是如果你想得到每个方向,那样:
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];
Run Code Online (Sandbox Code Playgroud)
向左滑动时将调用函数handleSwipeGestureLeft,向右滑动时将调用handleSwipeGestureRight
归档时间: |
|
查看次数: |
38253 次 |
最近记录: |