Используйте 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"];