UIView анимации в настольной игре - PullRequest
0 голосов
/ 12 декабря 2011

Здравствуйте, я пытаюсь создать змею и настольную игру лестничного типа.и я перемещаю фигуры игрока с помощью анимации UIView. Но это движется фигурами игрока с кратчайшим путем, который он нашел.Я хочу перемещать фигуры игрока по горизонтали, вертикали и диагонали. Я использовал ниже код

 [UIView animateWithDuration:1.0f
                                animations:^{
                                       playerOneImage.center = boardView.center;


                                       // Here you can disable the game play so that while animation is in progress, player cannot do other operations like rotating the dice, etc...
                                  }
                                  completion:^(BOOL finished){
                                       if(finished) {
                                           NSLog(@"Player moved to square:");

                                          // Here you can enable the game play that disabled when animation started...
                                      }
                                   } ];

Пожалуйста, помогите

1 Ответ

2 голосов
/ 12 декабря 2011

Используйте CAKeyframeAnimation на слое вида вместо использования +[UIView animateWithDuration:...].

Сначала вам может понадобиться добавить каркас QuartzCore к вашей цели.Если вы не знаете, как это сделать, прочитайте Как «добавить существующие платформы» в Xcode 4? Также добавьте #import <QuartzCore/QuartzCore.h> в заголовочный файл .pch вашего приложения.

СейчасВы можете анимировать вид вдоль пути.Сначала создайте UIPath для пути, по которому вы хотите следовать представлению.Этот пример переместит представление вправо на 50 пунктов, а затем вниз на 100 пунктов:

CGPoint position = self.playerOneImage.layer.position;
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:position];
position.x += 50; [path addLineToPoint:position];
position.y += 100; [path addLineToPoint:position];

Затем создайте CAKeyframeAnimation, который будет анимировать свойство position вдоль этого пути:

CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
animation.duration = 1.0f;
animation.path = path.CGPath;
animation.calculationMode = kCAAnimationPaced;

Существует множество опций для изменения скорости, с которой слой движется по пути - проверьте документы.

Далее необходимо установить конечную позицию слоя где вы хотите, чтобы это было после окончания анимации.Это кажется странным, но это абсолютно необходимо:

// Important: you must actually set the layer's position!
// Otherwise the animation will put it back at the start
// when the animation ends.  You should set the layer's
// final position, then add the animation.
self.playerOneImage.layer.position = position;

Наконец, добавьте анимацию к слою:

[self.playerOneImage.layer addAnimation:animation forKey:@"position"];

И все готово.

Все вместедля легкого вырезания / вставки:

CGPoint position = self.playerOneImage.layer.position;
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:position];
position.x += 50; [path addLineToPoint:position];
position.y += 100; [path addLineToPoint:position];
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
animation.duration = 1.0f;
animation.path = path.CGPath;
animation.calculationMode = kCAAnimationPaced;
// Important: you must actually set the layer's position!
// Otherwise the animation will put it back at the start
// when the animation ends.  You should set the layer's
// final position, then add the animation.
self.playerOneImage.layer.position = position;
[self.playerOneImage.layer addAnimation:animation forKey:@"position"];
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...