做了一个仿映客跑车动画,效果就是边跑边放大,过程中车轮子也在转。先来看一下效果:
大体的思路就是把汽车和轮子图片放大一个view中,给这个view加上改变位置和大小的动画,同时,车轮子自己执行transform旋转的动画。为了方便这里的动画加在了view层,加在layer层效率会搞一些,大家主要看一下思路,代码不多,几十行就ok,属性的声明我就不贴了:
- (void)viewDidLoad {[super viewDidLoad];_width_car = 150.0;_heigh_car = _width_car / 1.97;_animtionView = [[UIView alloc]init];_animtionView.frame = CGRectMake(-_width_car, 50, _width_car, _heigh_car);[self.view addSubview:_animtionView];_wheel_x = _width_car * 0.03;_wheel_y = _heigh_car * 0.2;_imgView_wheel = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"wheel"]];_imgView_wheel.frame = CGRectMake(_wheel_x, _wheel_y, 14, 24);[_animtionView addSubview:_imgView_wheel];//车轮每0.1秒执行一次旋转动画self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(playingAction:) userInfo:nil repeats:YES];_imgView_car = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"car"]];_imgView_car.frame = CGRectMake(0, 0, _width_car, _heigh_car);[_animtionView addSubview:_imgView_car];
}- (IBAction)starAnimtion:(id)sender {[UIView animateWithDuration:5 animations:^{_animtionView.frame = CGRectMake(375, 400, _width_car*2, _heigh_car*2);_imgView_wheel.frame = CGRectMake(_wheel_x*2, _wheel_y*2, 14*2, 24*2);_imgView_car.frame = CGRectMake(0, 0, _width_car*2, _heigh_car*2);} completion:^(BOOL finished) {_animtionView.frame = CGRectMake(-_width_car, 50, _width_car, _heigh_car);_imgView_wheel.frame = CGRectMake(_wheel_x, _wheel_y, 14, 24);_imgView_car.frame = CGRectMake(0, 0, _width_car, _heigh_car);}];
}
-(void)playingAction:(id)sender{_imgView_wheel.transform = CGAffineTransformRotate(_imgView_wheel.transform, M_PI);
}
别忘了销毁timer以防内存泄漏:
- (void)dealloc{
[_timer invalidate];_timer = nil;
}
最后给大家介绍一个做OC动画非常给力的软件QuartzCode,用类似flash的方法制作动画,并且可以生成Objective-C的layer层动画代码,非常方便,做复杂的动画非常高效,感兴趣的可以了解一下。