先创建一个图片imageView
self.imageView =[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tara4.jpg"]]
self.imageView.frame =CGRectMake(85, 400, 200, 200)
self.imageView.layer.cornerRadius =100
self.imageView.layer.masksToBounds=YES
[self.view addSubview:self.imageView]
1. UIView提供的动画
(1).// UIView 提供的第一种动画
[UIView animateWithDuration:3 animations:^{
self.imageView.frame =CGRectMake(100, 100, 100, 75);
}];
使 imageView 移动到新的位置上,并且把size改为新的
(2).UIView 的第二种
使 imageView 移动到新的位置上,并且把size改为新的,最后又回到原位置
[UIView animateWithDuration:5 animations:^{
self.imageView.frame =CGRectMake(30, 100, 300, 225)
self.imageView.alpha =0
} completion:^(BOOL finished) {
[UIView animateWithDuration:3 animations:^{
self.imageView.frame =CGRectMake(85, 400, 200, 150)
self.imageView.alpha =1
}]
}]
(3).UIView的第三种方法
可以重复 第二个参数:延迟, 第三个参数:动画类型
只重复动画的内容
[UIView animateWithDuration:5 delay:0.1 options:UIViewAnimationOptionRepeat animations:^{
self.imageView.frame =CGRectMake(30, 100, 300, 225)
self.imageView.alpha =0
} completion:^(BOOL finished) {
[UIView animateWithDuration:3 animations:^{
self.imageView.frame =CGRectMake(85, 400, 200, 150)
self.imageView.alpha =1
}]
}]
(4)UIView 第四种 颤抖动画
// 第三个参数:设置的越小,震动的越剧烈
// 第四个参数 :设置越大, 图片的初速度越大
[UIView animateWithDuration:5 delay:0.1 usingSpringWithDamping:0.1 initialSpringVelocity:10 options:UIViewAnimationOptionRepeat animations:^{
self.imageView.frame =CGRectMake(30, 100, 300, 225)
self.imageView.alpha =0
} completion:^(BOOL finished) {
[UIView animateWithDuration:3 animations:^{
self.imageView.frame =CGRectMake(85, 400, 200, 150)
self.imageView.alpha =1
}]
}]
(1).对视图进行旋转的操作
// 第二个参数:设置旋转的弧度 (π /4)
self.imageView.transform =CGAffineTransformRotate(self.imageView.transform, M_PI_4)
(2).对视图的缩放
// 第二, 三个参数:设置缩放的比例
self.imageView.transform = CGAffineTransformScale(self.imageView.transform, 0.9, 0.9)
(3).使视图偏移
设置视图的偏移量
self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, 50, 50)
3. layer动画
layer 主要负责显示控件的一些设置信息 ,比如边框 ,弧度等 ,layer动画的种类很多,我们看见的UIView的动画也是封装了几个layer动画
(1).创建一个动画的效果
CATransition *transition =[CATransition animation];
设置动画种类 : cube (), rippleEffect (水波纹) suckEffect oglFlip
transition.type =@"rippleEffect";
[transition setDuration:3];
[transition setRepeatCount:NSIntegerMax];
[self.imageView.layer addAnimation:transition forKey:@"transistion"];
(2).layer 动画的第二种 (放大缩小)
CABasicAnimation *basic =[CABasicAnimation animationWithKeyPath:@"transform.scale"];
[basic setDuration:3];
[basic setRepeatCount:NSIntegerMax];
缩放的初始倍数
basic.fromValue =[NSNumber numberWithInt:1];
缩放的结束倍数
basic.toValue = [NSNumber numberWithInt:2];
[self.imageView.layer addAnimation:basic forKey:@"basic"];
(3). 让图片旋转
CABasicAnimation *basic =[CABasicAnimation animationWithKeyPath:@"transform.rotation"];
basic.fromValue =[NSNumber numberWithInt:0.0];
basic.toValue =[NSNumber numberWithFloat: -4 *M_PI_2];
[basic setDuration:3];
[basic setRepeatCount:NSIntegerMax];
[self.imageView.layer addAnimation:basic forKey:@"basic"];
[basic setAutoreverses:YES];
(4). 关键帧动画
CAKeyframeAnimation *keyAnimation =[CAKeyframeAnimation animationWithKeyPath:@"position"];
CGMutablePathRef path =CGPathCreateMutable();
CGPathMoveToPoint(path,NULL, self.imageView.frame.origin.x, self.imageView.frame.origin.y);
CGPathAddLineToPoint(path, NULL, 100, 100);
CGPathAddLineToPoint(path, NULL, 300, 20);
CGPathAddLineToPoint(path, NULL, 50, 10);
CGPathAddLineToPoint(path, NULL, 140, 200);
CGPathAddCurveToPoint(path, NULL, 200, 200, 200, 100, 120, 40);
CGPathAddCurveToPoint(path, NULL, 80, 10, 20, 100, 300, 100);
CGPathAddCurveToPoint(path, NULL, 20, 90, 20, 100, 200, 150);
CGPathAddCurveToPoint(path, NULL, 30, 70, 60, 90, 110, 50);
[keyAnimation setDuration:3];
[keyAnimation setRepeatCount:NSIntegerMax];
[keyAnimation setPath:path];
[self.imageView.layer addAnimation:keyAnimation forKey:@"keyAnimation"];