animation: how to animate a view using layer?

旧城冷巷雨未停 提交于 2019-12-11 08:13:03

问题


Image showing what I would like to achieve

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;
}

回答1:


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.

Creating an explicit animation

CABasicAnimation *move = [CABasicAnimation animationWithKeyPath:@"position"];
move.toValue = [NSValue valueWithCGPoint:newPoint];
move.duration = 0.3;
[self.imageView.layer addAnimation:move forKey:@"myMoveAnimation"];

Using UIView animations

[UIView animateWithDuration:0.3 animations:^{
    self.imageView.center = newCenter;
}];


来源:https://stackoverflow.com/questions/12958041/animation-how-to-animate-a-view-using-layer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!