我们定义一个UIView属性view4animation来执行动画:
1. UIView动画块
//标识着动画的开始
[UIView beginAnimations:@"btn4kuai" context:nil];
//这里面写动画的内容
//动画持续时间
[UIView setAnimationDuration:1];
//改变颜色
self.view4animation.backgroundColor = [UIColor blueColor];
CGPoint center = self.view4animation.center;
center.x += 100;
center.y += 120;
//改变中心点
self.view4animation.center = center;
//改变透明度
self.view4animation.alpha = 1;
//动画结束时执行事件
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(changeBtn:)];
//提交动画,也就是动画结束了
[UIView commitAnimations];
- (void)changeBtn:(NSString *)str{
NSLog(@"view动画执行完毕");
[UIView beginAnimations:@"btn" context:nil];
//设置反复次数
//必须放在动画属性代码前面面
[UIView setAnimationRepeatCount:3];
[UIView setAnimationRepeatAutoreverses:YES];
[UIView setAnimationDuration:1];
CGPoint center = self.view4animation.center;
center.x -= 100;
self.view4animation.center = center;
[UIView commitAnimations];
}
2. 动画block语法
//参数1.动画时间 2.动画效果
[UIView animateWithDuration:1 animations:^{
//这写动画效果
self.view4animation.backgroundColor = [UIColor yellowColor];
self.view4animation.alpha = 0.3;
}];
[UIView animateWithDuration:2 animations:^{
//这写动画
CGPoint p = self.view4animation.center;
p.x += 200;
p.y += 200;
self.view4animation.center = p;
} completion:^(BOOL finished) {
//这是动画执行完之后在执行的,可以在这继续加动画
//返回动画
[UIView animateWithDuration:1 delay:1 options:UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse animations:^{
CGPoint p = self.view4animation.center;
p.x -= 200;
p.y -= 200;
self.view4animation.center = p;
} completion:^(BOOL finished) {
}];
}];
3 UIViewAnimationOptions动画
UIViewAnimationOptions是系统的一些例如翻转移出之类的动画,这里介绍一下翻页的效果:
UIView *newView = [[UIView alloc]initWithFrame:self.view.frame];
newView.tag = 100;
newView.backgroundColor = [UIColor blueColor];
//第一个参数是在哪个视图上进行翻页效果
//第二个参数是翻页持续时间
//第三个参数是翻页效果
[UIView transitionWithView:self.view.superview duration:1 options:UIViewAnimationOptionTransitionFlipFromBottom animations:^{
[self.view addSubview:newView];
} completion:^(BOOL finished) {
//执行结束
}];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleTap:)];
[newView addGestureRecognizer:singleTap];
- (void)singleTap:(UITapGestureRecognizer*)gesture{
[UIView transitionWithView:self.view.superview duration:1 options:UIViewAnimationOptionTransitionFlipFromBottom animations:^{
UIView *view = (UIView *)[self.view viewWithTag:100];
[view removeFromSuperview];
} completion:^(BOOL finished) {
//执行结束
}];
}
4. AffinaTransFrom动画
AffinaTransFrom动画常用的操作有偏移,缩放,旋转等操作,它只进行变换,并不带动画效果,需要与其他动画或者timer相配合使用。AffinaTransFrom分为2D仿射变换和3D仿射变换。顾名思义,2D仿射变换是在x轴和y轴上进行变换,3D仿射变换则引入了z轴,即与屏幕垂直的方向。
2D仿射变换旋转、缩放、平移:
[UIView animateWithDuration:1 animations:^{
//旋转
self.view4animation.transform = CGAffineTransformMakeRotation(M_PI);
//缩放参数是原来的多少倍,是一个比例值
self.view4animation.transform = CGAffineTransformMakeScale(0.5, 0.5);
//偏移
self.view4animation.transform = CGAffineTransformMakeTranslation(100, 100);
} completion:^(BOOL finished) {
[UIView animateWithDuration:1 animations:^{
//1.创建一个仿射变换
CGAffineTransform form = CGAffineTransformIdentity;
//2.向仿射变换里添加要做的操作
CGAffineTransformRotate(form, M_PI);
CGAffineTransformScale(form, 0.5, 0.5);
CGAffineTransformTranslate(form, -100, -100);
//3.将我们的仿射变换对象给我们的控件
self.view4animation.transform = form;
//这时就会有一个组合的效果了
}];
}];
3D仿射变换我们来做一个抢红包的动画,即旋转:
定义一个属性int属性:
@property (nonatomic, assign) int rotateNum;
_rotateNum = 1
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(starAnimate) userInfo:nil repeats:YES];
- (void)starOPenAnimate{
_openAnimateView.layer.transform = CATransform3DMakeRotation(M_PI/5*(_rotateNum++), 0, 1, 0);
}
至于CATransform3D的参数说明,这篇博客讲得很清楚:http://blog.sina.com.cn/s/blog_51a995b70101mz3q.html