//图片进行自动旋转
CABasicAnimation是一个最多只能有两个关键帧的动画,
UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"7"]];
imageView.frame = CGRectMake(40, 60, 200, 250);
[self.view addSubview:imageView];
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
animation.delegate = self;
animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 0, 0, 1.0)];
//执行时间
animation.duration = 30;
animation.cumulative = YES;//累积的
//执行次数
animation.repeatCount = INT_MAX;
animation.autoreverses=YES;//是否自动重复
[imageView.layer addAnimation:animation forKey:@"animation"];
CATransition动画使用了类型type和子类型subtype两个概念。type属性指定了过渡的种类(淡化、推挤、揭开、覆盖)。subtype设置了过渡的方向(从上、下、左、右)。另外,CATransition私有的动画类型有(立方体、吸收、翻转、波纹、翻页、反翻页、镜头开、镜头关)。
/* CATransition *myT = [ CATransition animation];
myT.timingFunction = UIViewAnimationCurveEaseInOut;
myT.type = @"cube";
//动画类型
// 1 kCATransitionFromBottom
// 2 kCATransitionFromLeft
// 3 kCATransitionFromTop
// 4 kCATransitionFromRight
//myT.subtype = kCATransitionFromLeft;
myT.subtype = kCATransitionFromLeft;
myT.duration = 0.5;
[self.navigationController.view.layer addAnimation: myT forKey:nil];
NSLog(@"app");*/
创建UIView动画(块)——(指过渡效果的动画)
//方法一:
//过度动画的效果,是2个View的切换
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton* button12 = [[UIButton alloc]initWithFrame:CGRectMake(20, 340, 150, 30)];
[button12 addTarget:self action:@selector(button22) forControlEvents:UIControlEventTouchUpInside];
[button12 setTitle:@"开始动画" forState:UIControlStateNormal];
button12.backgroundColor = [UIColor redColor];
[self.view addSubview:button12];
//方法二:
// 2 用block语法实现
/* [UIView animateWithDuration:0.5 animations:^{
CGRect frames = self.view_1.frame;
frames.origin.x = 160;
self.view_1.frame = frames;
//缩放
self.view_1.transform = CGAffineTransformScale(self.view_1.transform, 0.01, 0.01);
}completion:^(BOOL finished) {
CGRect frames = self.view_1.frame;
frames.origin.x = 0;
self.view_1.frame = frames;
//恢复到原始的缩放
self.view_1.transform = CGAffineTransformIdentity;
}];*/
}
-(void)button22
{
//开始动画
[UIView beginAnimations:@"testanimation" context:nil];
[UIView setAnimationDuration:0.5];
//代理
[UIView setAnimationDelegate:self];
//动画的响应事件,使视图自动回到原来的位置
[UIView setAnimationDidStopSelector:@selector(animationstop)];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
//获得视图的位置
CGRect frames = self.view_1.frame;
frames.origin.x = 160;
self.view_1.frame = frames;
[UIView commitAnimations];
}
-(void)animationstop
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
CGRect frames = self.view_1.frame;
frames.origin.x = 0;
self.view_1.frame = frames;
[UIView commitAnimations];
}