IOS动画+转场动画

IOS动画+转场动画


#import "ViewController.h"

#import "secondViewController.h"

#define kScreenWidth ([[UIScreen mainScreen] bounds].size.width)

#define kScreenHeight ([[UIScreen mainScreen] bounds].size.height)

@interface ViewController ()

@property (nonatomic,strong)UIImageView * img;

@end@implementation ViewController//如果fillMode=kCAFillModeForwards和removedOnComletion=NO,那么在动画执行完毕后,图层会保持显示动画执行后的状态。但在实质上,图层的属性值还是动画执行前的初始值,并没有真正被改变。//anima.fillMode = kCAFillModeForwards;//anima.removedOnCompletion = NO;

- (void)viewDidLoad {    

[super viewDidLoad];    

self.img = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];    

self.img.center =self.view.center;  

 // self.img.layer.masksToBounds =YES;    

self.img.layer.shadowColor = [UIColor lightGrayColor].CGColor;    

self.img.layer.shadowOffset = CGSizeMake(7, 7);    

self.img.layer.shadowOpacity = 0.70;   

 [self.view addSubview:self.img ];    

self.img.image = [UIImage imageNamed:@"img1.jpg"];    }

//position动画

- (void)positionup{    

CABasicAnimation * anima = [CABasicAnimation animationWithKeyPath:@"position"];    anima.fromValue = [NSValue valueWithCGPoint:CGPointMake(kScreenWidth/2, kScreenHeight/2)];    

anima.toValue  = [NSValue valueWithCGPoint:CGPointMake(kScreenWidth/2, kScreenHeight/2-300)];   

 anima.duration =1.0f;    

anima.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];   

 [self.img.layer addAnimation:anima forKey:@"positionAnimation"];}

-(void)positiondown{    

CABasicAnimation * anima = [CABasicAnimation animationWithKeyPath:@"position"];    anima.fromValue = [NSValue valueWithCGPoint:CGPointMake(kScreenWidth/2, kScreenHeight/2)];   

 anima.toValue  = [NSValue valueWithCGPoint:CGPointMake(kScreenWidth/2, kScreenHeight/2+300)];    

anima.duration = 2.0f;   

 anima.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];  

  [self.img.layer addAnimation:anima forKey:@"positionAnimation"];    }

- (void)positionleft{        

CABasicAnimation  * anima = [CABasicAnimation animationWithKeyPath:@"position"];    anima.fromValue = [NSValue valueWithCGPoint:CGPointMake(kScreenWidth/2, kScreenHeight/2)];    

anima.toValue  = [NSValue valueWithCGPoint:CGPointMake(kScreenWidth/2-200, kScreenHeight/2)];    

anima.duration =3.0f;    

anima.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];    

[self.img.layer addAnimation:anima forKey:@"position"];    }

- (void)positionright{    

CABasicAnimation * anima = [CABasicAnimation animationWithKeyPath:@"position"];    anima.fromValue  = [NSValue valueWithCGPoint:CGPointMake(kScreenWidth/2, kScreenHeight/2)];   

 anima.toValue    = [NSValue valueWithCGPoint:CGPointMake(kScreenWidth/2+200, kScreenHeight/2)];   

 anima.duration  = 3.0f;  

  [self.img.layer addAnimation:anima forKey:@"position"];    }

-(void)positionzheng{    

CAKeyframeAnimation * anima =  [CAKeyframeAnimation animationWithKeyPath:@"position"];    NSValue  * value1 = [NSValue valueWithCGPoint:CGPointMake(0, 0)];  

  NSValue  * value2 = [NSValue valueWithCGPoint:CGPointMake(200, 200)];  

  NSValue  * value3 = [NSValue valueWithCGPoint:CGPointMake(200, 400)];   

 NSValue  * value4 = [NSValue valueWithCGPoint:CGPointMake(0, 200)];    anima.values = @[value1,value2,value3,value4];    

anima.duration = 2.0f;      

anima.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];   

 [self.img.layer addAnimation:anima forKey:@"positionAnimation"];    }

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{    

self.img.hidden = YES;}

- (void)positionbezierpath{    

CAKeyframeAnimation * anima = [CAKeyframeAnimation animationWithKeyPath:@"position"];    UIBezierPath    * path = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 200, 500)];    anima.path = path.CGPath;    

anima.duration = 4.0f;   

 anima.fillMode = kCAFillModeForwards;    

anima.removedOnCompletion = NO;  

  anima.delegate = self;  

  [self.img.layer addAnimation:anima forKey:@"position"];}

//opacity动画

- (void)opacity{    CABasicAnimation * anima = [CABasicAnimation animationWithKeyPath:@"opacity"];   

 anima.fromValue = [NSNumber numberWithFloat:1.0f];  

 anima.toValue  = [NSNumber numberWithFloat:0.2f];    

anima.fillMode = kCAFillModeForwards;    

anima.removedOnCompletion = NO;   

 anima.duration =1.0f;    

[self.img.layer addAnimation:anima forKey:@"opacity"];    }

//transfrom.scale动画

- (void)transformscale{    

CABasicAnimation * anima = [CABasicAnimation animationWithKeyPath:@"transform.scale"];    anima.toValue  = [NSNumber numberWithFloat:2.0f];   

 anima.duration = 3.0f;    

anima.fillMode = kCAFillModeForwards;    

anima.removedOnCompletion = NO;   

 [self.img.layer addAnimation:anima forKey:@"transform.scale"];}

- (void)transformscalex{   

 CABasicAnimation * anima = [CABasicAnimation animationWithKeyPath:@"transform.scale.x"];    anima.toValue = [NSNumber numberWithFloat:1.0f];    

anima.duration = 2.0f;  

  [self.img.layer addAnimation:anima forKey:@"transform.scale.x"];}

- (void)transformscaley{   

 CABasicAnimation * anima = [CABasicAnimation  animationWithKeyPath:@"transform.scale.y"];    anima.toValue = [NSNumber numberWithFloat:1.0f];  

  anima.duration = 2.0f;    

[self.img.layer addAnimation:anima forKey:@"transform.scale.y"];    }

//transform.rotation动画

- (void)rotationz{   

 CABasicAnimation * anima = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];  

  anima.toValue = [NSNumber numberWithFloat:M_PI*2];    

anima.duration =2.0f;  

  [self.img.layer addAnimation:anima forKey:@"transform.rotation.z"];    }

- (void)rotationx{   

 CABasicAnimation * anima = [CABasicAnimation animationWithKeyPath:@"transform.rotation.x"];    

anima.toValue = [NSNumber numberWithFloat:M_PI*2];    

anima.duration = 2.0f;   

 [self.img.layer addAnimation:anima forKey:@"transform.rotation.x"];}

- (void)rotationy{   

 CABasicAnimation  * anima  = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];    

anima.toValue = [NSNumber numberWithFloat:M_PI*2];    

anima.duration = 2.0f;   

 [self.img.layer addAnimation:anima forKey:@"transform.scale.y"];}

- (void)shake{    

CAKeyframeAnimation  * anima = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation"];    

NSValue * value1 = [NSNumber numberWithFloat:-M_PI/180*4];    

NSValue * value2 = [NSNumber numberWithFloat:M_PI/180*4];    

NSValue * value3 = [NSNumber numberWithFloat:-M_PI/180*4];   

 anima.values = @[value1,value2,value3];    

anima.repeatCount = MAXFLOAT;   

 [self.img.layer addAnimation:anima forKey:@"transform.rotation"];}

//backgroundColor动画

- (void)backgroundColor{  

  CABasicAnimation * anima = [CABasicAnimation  animationWithKeyPath:@"backgroundColor"];    anima.toValue = (id)[UIColor grayColor].CGColor;    

anima.duration =2.0f;   

 [self.img.layer addAnimation:anima forKey:@"backgroundColor"];}

//cornerRadius动画

- (void)cornerRadius{    

CABasicAnimation  * anima =  [CABasicAnimation animationWithKeyPath:@"cornerRadius"];    anima.toValue  = [NSNumber numberWithFloat:50.0f];  

  anima.duration = 1.0f;    

anima.fillMode = kCAFillModeForwards;   

 anima.removedOnCompletion = NO;   

 [self.img.layer addAnimation:anima forKey:@"cornerRadius"];}

- (void)cornerRadiusl{   

 CABasicAnimation * anima = [CABasicAnimation animationWithKeyPath:@"cornerRadius"];    anima.toValue = [NSNumber  numberWithFloat:0];    

anima.duration =2.0;    

anima.fillMode = kCAFillModeForwards;   

 anima.removedOnCompletion = NO; 

   [self.img.layer addAnimation:anima forKey:@"cornerRadius"];}

//图品切换动画

- (void)changeImageAnimatedWithView :(UIImageView *)imageV AndImage:(UIImage*)image{    CATransition * transition  = [CATransition  animation];   

 transition.duration = 2;    

transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];   

 transition.type = @"cameraIrisHollowClose";//kCATransitionReveal;  

  [imageV.layer addAnimation:transition forKey:@"transition"];  

 [imageV setImage:image];}

//borderWidth动画

- (void)borderWidth{    

CABasicAnimation * anima = [CABasicAnimation animationWithKeyPath:@"borderWidth"];    anima.toValue = [NSNumber numberWithFloat:20.0f]; 

   anima.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault];  

  anima.duration = 1.0f;    anima.fillMode = kCAFillModeForwards;    

anima.removedOnCompletion = NO;  

  [self.img.layer addAnimation:anima forKey:@"borderWidth"];}

// shadowColor动画

- (void)shadowColor{   

 CABasicAnimation * anima = [CABasicAnimation animationWithKeyPath:@"shadowColor"];    anima.toValue = (id)[UIColor redColor].CGColor;   

 anima.fillMode= kCAFillModeForwards;    

anima.removedOnCompletion =NO;    

anima.duration = 2.0f;   

 [self.img.layer addAnimation:anima forKey:@"shadowColor"];}

- (void)groupAnimation{    

CAKeyframeAnimation * anima = [CAKeyframeAnimation  animationWithKeyPath:@"position"];    NSValue * value = [NSValue valueWithCGPoint:CGPointMake(0, 0)]; 

   NSValue * value1 = [NSValue valueWithCGPoint:CGPointMake(20, 20)];    

anima.values = @[value,value1];       

 CABasicAnimation * anima1 = [CABasicAnimation animationWithKeyPath:@"opacity"];    anima1.fromValue = [NSNumber numberWithFloat:1.0f];    

anima1.toValue = [NSNumber numberWithFloat:0.2f];       

 CABasicAnimation  * anima2 = [CABasicAnimation  animationWithKeyPath:@"transform.rotation"];   

 anima2.toValue = [NSNumber numberWithFloat:M_PI*4];       

 CAAnimationGroup * group = [CAAnimationGroup animation];  

  group.animations = @[anima,anima1,anima2];   

 group.duration = 6.0f;    [self.img.layer addAnimation:group forKey:@"animation"];  

  //-如下,使用三个animation不分装成group,只是把他们添加到layer,也有组动画的效果。----------    /**    

 CAKeyframeAnimation *anima1 = [CAKeyframeAnimation animationWithKeyPath:@"position"];   

 NSValue * value = [NSValue valueWithCGPoint:CGPointMake(0, 0)];  

  NSValue * value1 = [NSValue valueWithCGPoint:CGPointMake(20, 20)];    

      anima1.values = @[value,value1];    

 anima1.duration = 4.0f;   

 [_wsView.layer addAnimation:anima1 forKey:@"aa"];      

    //缩放动画  

  CABasicAnimation *anima2 = [CABasicAnimation animationWithKeyPath:@"transform.scale"];    

 anima2.fromValue = [NSNumber numberWithFloat:0.8f];   

 anima2.toValue = [NSNumber numberWithFloat:2.0f];    

 anima2.duration = 4.0f;  

  [_wsView.layer addAnimation:anima2 forKey:@"bb"];         

 //旋转动画   

 CABasicAnimation *anima3 = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];    anima3.toValue = [NSNumber numberWithFloat:M_PI*4];  

  anima3.duration = 4.0f;    [_wsView.layer addAnimation:anima3 forKey:@"cc"];          */    }

-  (void)timeGroupAnimation{   

 CFTimeInterval currentTime = CACurrentMediaTime();    

CFTimeInterval time = CACurrentMediaTime();    

NSLog(@"%f",time);    //位移动画    //位移动画  

  CAKeyframeAnimation *anima1 = [CAKeyframeAnimation animationWithKeyPath:@"position"];    NSValue *value0 = [NSValue valueWithCGPoint:CGPointMake(0, kScreenHeight/2-50)];    NSValue *value1 = [NSValue valueWithCGPoint:CGPointMake(kScreenWidth/4, kScreenHeight/2-50)];   

 NSValue *value2 = [NSValue valueWithCGPoint:CGPointMake(kScreenWidth/4, kScreenHeight/2+50)];   

 NSValue *value3 = [NSValue valueWithCGPoint:CGPointMake(kScreenWidth/2, kScreenHeight/2+50)];   

 NSValue *value4 = [NSValue valueWithCGPoint:CGPointMake(kScreenWidth/2, kScreenHeight/2-50)];    

anima1.values = [NSArray arrayWithObjects:value0,value1,value2,value3,value4, nil];    anima1.beginTime = currentTime;    

anima1.duration = 2.0f;   

 anima1.fillMode = kCAFillModeForwards;  

  anima1.removedOnCompletion = NO;   

 [self.img.layer addAnimation:anima1 forKey:@"aa"];      

  //缩放动画    CABasicAnimation *anima2 = [CABasicAnimation animationWithKeyPath:@"transform.scale"];   

 anima2.fromValue = [NSNumber numberWithFloat:0.8f];  

  anima2.toValue = [NSNumber numberWithFloat:2.0f];   

 anima2.beginTime = currentTime+2.0f;  

  anima2.duration = 1.0f;    

anima2.fillMode = kCAFillModeForwards;    

anima2.removedOnCompletion = NO;  

  [self.img.layer addAnimation:anima2 forKey:@"bb"];       

 //旋转动画   

 CABasicAnimation *anima3 = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];   

 anima3.toValue = [NSNumber numberWithFloat:M_PI*4];   

 anima3.beginTime = currentTime+3.0f;    

anima3.duration = 1.0f;   

 anima3.fillMode = kCAFillModeForwards;  

  anima3.removedOnCompletion = NO;   

 [self.img.layer addAnimation:anima3 forKey:@"cc"];}

-(void)CATransition{   

 CATransition  * anima = [CATransition  animation];    

anima.type = kCAAnimationCubic;    

anima.duration  = 2.0f;    //   

 anima.subtype = kCATransitionFromRight; //设置动画的方向  

  [self.img setImage:[UIImage imageNamed:@"img2.jpg"]];   

 [self.img.layer addAnimation:anima forKey:@"catransition"];}

-(void)privateCATransition{    

CATransition *anima = [CATransition animation];    

anima.type = @"cube";//设置动画的类型    

anima.subtype = kCATransitionFromRight; //设置动画的方向    

anima.duration = 1.0f;        

[self.img.layer addAnimation:anima forKey:@"revealAnimation"];}

//私有api/*fade交叉淡化 过度的效果push新视图把旧视图推出去moveIn新视图移到旧视图上面reveal将旧视图移开,显示新视图cube立方体翻滚的效果oglFlip上下左右翻转的效果suckEffect收缩效果,如一块布被抽走rippleEffect水滴效果(像是在屏幕上滴了一滴水,然后泛起一下涟漪)pageCurl向上翻页效果pageUnCurl向下翻页效果cameraIrisHollowOpen相机镜头打开效果cameraIrisHollowClose相机镜头关闭效果*/

- (void)viewanimation{    self.img.transform = CGAffineTransformIdentity; 

   [UIView animateWithDuration:2.0f animations:^{       

 //移动        self.img.transform = CGAffineTransformMakeTranslation(100, 0);      

  //旋转        self.img.transform = CGAffineTransformMakeRotation(M_PI);    

    //缩放        self.img.transform = CGAffineTransformMakeScale(1, 2);            }];  

  //    [UIView animateWithDuration:2.0f animations:^{

//        

//    } completion:^(BOOL finished) {

//       

 //    }];//  

  //    [UIView animateWithDuration:2.0f

//                          delay:2.0f

//                        options:UIViewAnimationOptionCurveEaseInOut

//                    animations:^{

//      

  //    } completion:^(BOOL finished) {

//       

 //    }];

//    

//    [UIView animateKeyframesWithDuration:2.0f

//                                  delay:2.0f

//                                options:UIViewKeyframeAnimationOptionAutoreverse//                              animations:^{

//        

//    } completion:^(BOOL finished) {

//       

 //    }];}//动画组

- (void)viewGroup{    

self.img.transform = CGAffineTransformIdentity;  

  [UIView animateWithDuration:2.0 animations:^{       

 CGAffineTransform form = CGAffineTransformMakeScale(2, 2);        

CGAffineTransform form1 = CGAffineTransformRotate(form,M_PI);              

  self.img.transform = CGAffineTransformInvert(form1);    }];}

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event{

[self changeImageAnimatedWithView:self.img AndImage:[UIImage imageNamed:@"img2.jpg"]];

//[self timeGroupAnimation];

//[self shadowColor];

//[self CATransition];

//[self viewanimation];

// [self viewGroup];

}

//转场动画

- (void)viewCheak{

secondViewController * vic = [[secondViewController alloc]init];

dispatch_group_t group = dispatch_group_create();

dispatch_group_enter(group);

CATransition *transition = [[CATransition alloc]init];

transition.duration = .5;

transition.type =  @"rippleEffect";

transition.subtype = kCATransitionFromRight;

[self.navigationController.view.layer addAnimation:transition forKey:nil];

dispatch_group_leave(group);

dispatch_async(dispatch_get_main_queue(), ^{

[self.navigationController pushViewController:vic animated:NO];

});

}

@end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,602评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,442评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 152,878评论 0 344
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,306评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,330评论 5 373
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,071评论 1 285
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,382评论 3 400
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,006评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,512评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,965评论 2 325
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,094评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,732评论 4 323
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,283评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,286评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,512评论 1 262
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,536评论 2 354
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,828评论 2 345

推荐阅读更多精彩内容