针对iOS 动画做一下总结
- 基本动画
- 帧动画
- 组动画
- 转场动画
!- - !插一句UIview代码块(能实现最简单的动画)
_demoView.frame = CGRectMake(0, SCREEN_HEIGHT/2-50, 50, 50);
[UIView animateWithDuration:1.0f animations:^{
_demoView.frame = CGRectMake(SCREEN_WIDTH, SCREEN_HEIGHT/2-50, 50, 50);
} completion:^(BOOL finished) {
_demoView.frame = CGRectMake(SCREEN_WIDTH/2-25, SCREEN_HEIGHT/2-50, 50, 50);
}];
1.基本动画
- 第一
CABasicAnimation *anima = [CABasicAnimation animationWithKeyPath:@"position"];
anima.fromValue = [NSValue valueWithCGPoint:CGPointMake(0, SCREEN_HEIGHT/2-75)];
anima.toValue = [NSValue valueWithCGPoint:CGPointMake(SCREEN_WIDTH, SCREEN_HEIGHT/2-75)];
anima.duration = 1.0f;
[_demoView.layer addAnimation:anima forKey:@"positionAnimation"];
- 第二
var loginButton:UIButton?
var index:Int = 1;
override func viewDidLoad() {
super.viewDidLoad()
loginButton = UIButton(frame: CGRect(x: 20, y: 230, width: self.view.frame.width-20*2,height: 50))
loginButton!.backgroundColor = UIColor(red: 50/255.0, green: 185/255.0, blue: 170/255.0, alpha: 1.0)
loginButton!.setTitle("登陆", for: UIControlState())
self.view.addSubview(loginButton!)
}
override func viewWillAppear(_ animated: Bool) {
// 几何形状:拉伸 压缩效果
UIView.beginAnimations(nil, context: nil)//动画开始
UIView.setAnimationDuration(1)//动画周期设置
loginButton!.bounds = CGRect(x:0, y:0, width:loginButton!.frame.size.width*0.7, height:loginButton!.frame.size.height*1.2)//动画形状
UIView.commitAnimations()//动画提交
}
2.帧动画
// 1.做什么动画
CAKeyframeAnimation* anim = [[CAKeyframeAnimation alloc] init];
// 2.怎么做动画
anim.keyPath = @"position";
// -----------
// NSValue* v1 = [NSValue valueWithCGPoint:CGPointMake(100, 100)];
// NSValue* v2 = [NSValue valueWithCGPoint:CGPointMake(150, 100)];
// NSValue* v3 = [NSValue valueWithCGPoint:CGPointMake(100, 150)];
// NSValue* v4 = [NSValue valueWithCGPoint:CGPointMake(150, 150)];
//
// anim.values = @[ v1, v2, v3, v4 ]; // 关键的数据
// -----------
// 创建路径
UIBezierPath* path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(150, 150) radius:100 startAngle:0 endAngle:2 * M_PI clockwise:1];
anim.path = path.CGPath; // 路径
anim.duration = 2; // 时间
anim.repeatCount = INT_MAX; // 重复次数
// 3.对谁做动画
[self.layer addAnimation:anim forKey:nil];
3.组动画
// 1.创建动画
CAAnimationGroup* group = [[CAAnimationGroup alloc] init];
// ------ 基本动画(自旋转) ------
// 1.创建动画对象(做什么动画)
CABasicAnimation* anim = [[CABasicAnimation alloc] init];
// 2.怎么做动画
anim.keyPath = @"transform.rotation";
anim.byValue = @(2 * M_PI * 5); // 在自身的基础上增加
// ------ 基本动画(自旋转) ------
// ------ 关键帧动画(绕着圆转) ------
// 1.做什么动画
CAKeyframeAnimation* anim1 = [[CAKeyframeAnimation alloc] init];
// 2.怎么做动画
anim1.keyPath = @"position";
// 创建路径
UIBezierPath* path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(150, 150) radius:100 startAngle:0 endAngle:2 * M_PI clockwise:1];
anim1.path = path.CGPath; // 路径
// ------ 关键帧动画(绕着圆转) ------
// 2.操作
group.animations = @[ anim, anim1 ];
// 时间
group.duration = 3;
// 重复次数
group.repeatCount = INT_MAX;
// 3.添加动画
[self.layer addAnimation:group forKey:nil];
4.转场动画
// 1.创建动画
CATransition* anim = [[CATransition alloc] init];
// 2.操作
anim.type = @"moveIn";
if (sender.direction == UISwipeGestureRecognizerDirectionLeft) {
// 从右往左
// 方向
anim.subtype = kCATransitionFromRight;
}
else if (sender.direction == UISwipeGestureRecognizerDirectionRight) {
// 从左往右
// 方向
anim.subtype = kCATransitionFromLeft;
}
// 3.添加动画
[self.imageView.layer addAnimation:anim forKey:nil];