今天开发遇到一个缩放的动画,总结了两种常用的方式
UIView animation-duration-completion 方法和CoreAnimation
UIView代码:
[UIView animateWithDuration:10.0 animations:^{
_iconImgView.center = _iconImgView.center;
CGPoint imagePoint = _iconImgView.frame.origin;
CGSize imageSize = _iconImgView.frame.size;
_iconImgView.frame = CGRectMake(imagePoint.x, imagePoint.y, imageSize.width * 2, imageSize.height * 2);
}];
原点不变的放大到2倍感觉功能强,可以做任何动画但很烦琐
CoreAnimation代码:
CABasicAnimation * animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
//动画初始位置
animation.fromValue = @1;
//动画结束位置
animation.toValue = @2;
//设置动画持续时间
animation.duration = 1.0;
[_iconImgView.layer addAnimation:animation forKey:nil];
imageview 中心不变放大2倍
但是CoreAnimation 动画最后又回到原来的大小了
最后添加代码:
// 动画终了后不返回初始状态
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
解决问题!