如果我们开发的需求不是很特殊,IOS给我们提供的UI层的动画API就够我们开发用的了,比如UIView.animationWithDuration,UIView.transationFromView,
UIView.animateKeyFramesWithDuration,然后在利用工具类CFAffineTransFormXX就得实现我们比较常见的动画了,
一下是各种动画的练习
- (IBAction)commAnimation:(id)sender {
//最原始的原始动画
[UIViewbeginAnimations:@"Common"context:nil];
[UIViewsetAnimationDelay:1];
[UIViewsetAnimationDuration:1.0];
[UIViewsetAnimationDelegate:self];
[UIViewsetAnimationRepeatCount:2];
[UIViewsetAnimationWillStartSelector:@selector(startAni:)];
[UIViewsetAnimationDidStopSelector:@selector(stopAni:)];
[UIViewsetAnimationCurve:UIViewAnimationCurveEaseOut];
// self.mImagView.frame=self.mTextView.frame;更改中心就是移动
// self.mImagView.bounds=self.view.bounds; 更改bounds
// self.mImagView.backgroundColor=[UIColor redColor];更改背景颜色
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.mImagView cache:YES];//旋转或者翻页效果动画
[UIViewcommitAnimations];
//转场动画fromView要消失的 toView是要进入视图的
[UIView transitionFromView:_mTextView toView:_mImagView duration:2 options:UIViewAnimationOptionCurveEaseOut|UIViewAnimationTransitionCurlDown completion:nil];
}
- (IBAction)blockAnimation:(id)sender {
//IOS7.0以后加入的Spring动画支持
[UIViewanimateWithDuration:2.0fdelay:0.5fusingSpringWithDamping:0.5initialSpringVelocity:0.25options:UIViewAnimationOptionCurveEaseOutanimations:^{
CGFloat orX=self.mTextView.center.x;
CGFloat orY=self.mTextView.center.y;
[self.mTextView setCenter:CGPointMake(orX+100, orY+100)];
self.mLable.alpha=0.25;
self.mImagView.transform=CGAffineTransformScale(self.mImagView.transform,0.5,0.5);
} completion:^(BOOL finished) {
NSLog(@"第一个动画完毕");
} ];
}
//帧动画对于普通的动画来说也算是复杂的了
- (IBAction)framesAnimation:(id)sender {
self.mImagView.image=nil;
[UIView animateKeyframesWithDuration:9.0 delay:0.f options:UIViewKeyframeAnimationOptionCalculationModeLinear animations:^{
[UIView addKeyframeWithRelativeStartTime:0.f relativeDuration:1.0/4 animations:^{
self.mImagView.backgroundColor=[UIColor colorWithRed:0.9475 green:0.1921 blue:0.1746 alpha:1.0];
}];
[UIView addKeyframeWithRelativeStartTime:1.0/4 relativeDuration:1.0/4 animations:^{
self.mImagView.backgroundColor=[UIColor colorWithRed:0.1064 green:0.6052 blue:0.0334 alpha:1.0];
}];
[UIView addKeyframeWithRelativeStartTime:2.0/4 relativeDuration:1.0/4 animations:^{
self.mImagView.backgroundColor=[UIColor colorWithRed:0.1366 green:0.3017 blue:0.8411 alpha:1.0];
}];
[UIView addKeyframeWithRelativeStartTime:3.0/4 relativeDuration:1.0/4 animations:^{
self.mImagView.backgroundColor=[UIColor colorWithRed:0.619 green:0.037 blue:0.6719 alpha:1.0];
}];
}completion:^(BOOL finished){
NSLog(@"动画结束");
}];
CGFloat orX=self.mLable.center.x;
CGFloat orY=self.mLable.center.y;
CGPoint orCenter=self.mLable.center;
[UIViewanimateKeyframesWithDuration:2delay:0.5options:UIViewKeyframeAnimationOptionRepeatanimations:^{
[UIViewaddKeyframeWithRelativeStartTime:0.0relativeDuration:0.25animations:^{
[self.mLable setCenter:CGPointMake(orX+80, orY-20)];
}];
[UIViewaddKeyframeWithRelativeStartTime:0.15relativeDuration:0.4animations:^{
self.mLable.transform=CGAffineTransformMakeRotation(-M_PI_4/2);
}];
[UIViewaddKeyframeWithRelativeStartTime:0.25relativeDuration:0.35animations:^{
[self.mLable setCenter:CGPointMake(orX+100, orY-50)];
self.mLable.alpha=0;
}];
[UIViewaddKeyframeWithRelativeStartTime:0.55relativeDuration:0.05animations:^{
self.mLable.transform=CGAffineTransformIdentity;
[self.mLable setCenter:CGPointMake(0, orY)];
}];
[UIViewaddKeyframeWithRelativeStartTime:0.65relativeDuration:0.5animations:^{
self.mLable.alpha=1.0;
self.mLable.center=orCenter;
}];
} completion:^(BOOL finished) {
}];
}
但是如果我们要想实现比较复杂的动画还是必须要了解和学习IOS CoreAnimation的。
其实我们动画刷新UI使用的CADisplayLink,其实他是一个专门用来刷新UI的 NSTimer使用起来比较简单,
-(void)useCADisplayLink{
CADisplayLink *cADisplayLink=[