#import "QuartzCore/QuartzCore.h"
@interface mainVC()
{
BOOL isPause;
UIButton *controlButton;
}
-(void)animationOfUIKit;
-(void)animationOfBlock;
-(void)animationOfCABasicAnimation;
-(void)animationOfCAKeyframeAnimation;
-(void)animationOfCAKeyframeAnimationPath;
@end
@implementation mainVC
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
-(void)viewDidLoad
{
[super viewDidLoad];
controlButton = [UIButton buttonWithType:UIButtonTypeSystem];
[controlButton setFrame:CGRectMake(20, 200, 60, 40)];
[controlButton addTarget:self
action:@selector(clickControlButton:)
forControlEvents:UIControlEventTouchUpInside];
[controlButton setTitle:@"暂停" forState:UIControlStateNormal];
[self.view addSubview:controlButton];
//使用上下文设置UIKit动画
//[self animationOfUIKit];
//通过使用代码块设置动画
//[self animationOfBlock];
//使用CABasicAnimation
//[self animationOfCABasicAnimation ];
//使用CAKeyframeAnimation动画
//[self animationOfCAKeyframeAnimation];
//路径动画
[self animationOfCAKeyframeAnimationPath ];
}
-(void)animationOfUIKit
{
UIView *redView=[[UIView alloc]initWithFrame:CGRectMake(10, 10, 100, 100)];
redView.backgroundColor=[UIColor redColor];
[self.view addSubview:redView];
//开始动画
[UIView beginAnimations:@"test" context:nil];
//动画时长
[UIView setAnimationDuration:1];
/*
*要进行动画设置的地方
*/
redView.backgroundColor=[UIColor blueColor];
redView.frame=CGRectMake(50, 50, 200, 200);
redView.alpha=0.5;
//动画结束
[UIView commitAnimations];
}
-(void)animationOfBlock
{
//初始化一个View,用来显示动画
UIView *redView=[[UIView alloc]initWithFrame:CGRectMake(10, 10, 100, 100)];
redView.backgroundColor=[UIColor redColor];
[self.view addSubview:redView];
[UIView animateWithDuration:1 //时长
delay:0 //延迟时间
options:UIViewAnimationOptionTransitionFlipFromLeft//动画效果
animations:^{
//动画设置区域
redView.backgroundColor=[UIColor blueColor];
redView.frame=CGRectMake(50, 50, 200, 200);
redView.alpha=0.5;
} completion:^(BOOL finish){
//动画结束时调用
//............
}];
}
-(void)animationOfCABasicAnimation
{
//创建一个CABasicAnimation对象
CABasicAnimation *animation=[CABasicAnimation animation];
//设置颜色
animation.toValue=(id)[UIColor blueColor].CGColor;
//动画时间
animation.duration=1;
//是否反转变为原来的属性值
animation.autoreverses=YES;
//把animation添加到图层的layer中,便可以播放动画了。forKey指定要应用此动画的属性
[self.view.layer addAnimation:animation forKey:@"backgroundColor"];
}
-(void)animationOfCAKeyframeAnimation
{
//初始化一个View,用来显示动画
UIView *redView=[[UIView alloc]initWithFrame:CGRectMake(10, 30, 100, 100)];
redView.backgroundColor=[UIColor redColor];
[self.view addSubview:redView];
CAKeyframeAnimation *animation=[CAKeyframeAnimation animationWithKeyPath:@"test"];
//设置属性值
animation.values=[NSArray arrayWithObjects:
(id)self.view.backgroundColor,
(id)[UIColor yellowColor],
(id)[UIColor greenColor],
(id)[UIColor blueColor],nil];
animation.duration=3;
animation.autoreverses=YES;
//把关键帧添加到layer中
[redView.layer addAnimation:animation forKey:@"backgroundColor"];
}
-(void)animationOfCAKeyframeAnimationPath
{
//初始化一个View,用来显示动画
UIView *redView=[[UIView alloc]initWithFrame:CGRectMake(10, 30, 20, 20)];
redView.backgroundColor=[UIColor redColor];
redView.tag = 111;
[self.view addSubview:redView];
CAKeyframeAnimation *ani=[CAKeyframeAnimation animation];
CGMutablePathRef aPath=CGPathCreateMutable();
CGPathMoveToPoint(aPath, nil, 20, 20);
CGPathAddCurveToPoint(aPath, nil,
160, 30,
220, 220,
240, 380);
ani.path=aPath;
ani.duration=10;
ani.repeatCount=HUGE_VAL;
ani.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
ani.rotationMode=@"auto";
[redView.layer addAnimation:ani forKey:@"position"];
}
//暂停layer上面的动画
- (void)pauseLayer:(CALayer*)layer
{
CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
layer.speed = 0.0;
layer.timeOffset = pausedTime;
}
//继续layer上面的动画
- (void)resumeLayer:(CALayer*)layer
{
CFTimeInterval pausedTime = [layer timeOffset];
layer.speed = 1.0;
layer.timeOffset = 0.0;
layer.beginTime = 0.0;
CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
layer.beginTime = timeSincePause;
}
- (void)pauseSoccer
{
isPause = YES;
[controlButton setTitle:@"继续" forState:UIControlStateNormal];
[self pauseLayer:[self.view viewWithTag:111].layer];
}
- (void)resumeSoccer
{
isPause = NO;
[controlButton setTitle:@"暂停" forState:UIControlStateNormal];
[self resumeLayer:[self.view viewWithTag:111].layer];
}
- (void)clickControlButton:(id)sender {
if (isPause) {
[self resumeSoccer];
}else{
[self pauseSoccer];
}
}
@end