动画:如何使用图层为视图设置动画?

use*_*717 1 core-animation ios

图像显示了我想要实现的目标

我想将一个视图从一个位置动态移动到一个新的位置.如果更新视图的图层位置,我认为视图将动画移动到新位置,implicit animation我猜,但是没有动画,为什么?

- (IBAction)startAnimation:(id)sender {
    CGPoint position = self.imageView.layer.position;
    position.y += 90;
    self.imageView.layer.position = position;
}
Run Code Online (Sandbox Code Playgroud)

Dav*_*ist 6

隐式动画仅发生在独立图层上,而不是备份视图的图层.您需要明确动画该位置.您可以通过为位置创建CABasicAnimation并将其添加到图层或使用UIView动画为视图的center属性设置动画来实现.

创建显式动画

CABasicAnimation *move = [CABasicAnimation animationWithKeyPath:@"position"];
move.toValue = [NSValue valueWithCGPoint:newPoint];
move.duration = 0.3;
[self.imageView.layer addAnimation:move forKey:@"myMoveAnimation"];
Run Code Online (Sandbox Code Playgroud)

使用UIView动画

[UIView animateWithDuration:0.3 animations:^{
    self.imageView.center = newCenter;
}];
Run Code Online (Sandbox Code Playgroud)