动画:如何使用图层为视图设置动画? [英] animation: how to animate a view using layer?
问题描述
我想动态地将视图从一个位置移动到一个新位置。如果更新视图的图层位置,我认为视图会动态地移动到新位置,我猜这是隐式动画
,但是没有动画,为什么?
I want to move a view animately from a position to a new postion. If update the layer position of a view, I think the view will animately move to the new position, which is implicit animation
I guess, but there's no animation, why?
- (IBAction)startAnimation:(id)sender {
CGPoint position = self.imageView.layer.position;
position.y += 90;
self.imageView.layer.position = position;
}
推荐答案
隐式动画仅在独立的情况下发生层,而不是层备份视图。您将需要显式设置该位置的动画。您可以通过为该位置创建一个CABasicAnimation并将其添加到图层中来执行此操作,也可以使用UIView动画为视图的center属性设置动画。
Implicit animations only happen for standalone layer, not layers backing up views. You will need to explicitly animate the position. You can either do it by creating a CABasicAnimation for the position and add it to the layer or use UIView animations to animate the center property of the view.
CABasicAnimation *move = [CABasicAnimation animationWithKeyPath:@"position"];
move.toValue = [NSValue valueWithCGPoint:newPoint];
move.duration = 0.3;
[self.imageView.layer addAnimation:move forKey:@"myMoveAnimation"];
使用UIView动画
Using UIView animations
[UIView animateWithDuration:0.3 animations:^{
self.imageView.center = newCenter;
}];
这篇关于动画:如何使用图层为视图设置动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!