动画

 

动画 主要是在视图上过渡效果;提高用户的体验

//翻转的动画

[UIView beginAnimations:nil context:nil];动画开始

[UIView setAnimationDuration:1];//设置时长

[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];//设置动画淡入浅出

[UIView setAnimationDelegate:self];//设置代理

[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:manImageView cache:YES];//设置翻转方向

.......设置各种动画属性

[UIView commitAnimations];动画结束

//旋转动画

CGAffineTransform transform;//创建一个CGAffineTransform transform对象

transform = CGAffineTransformRotate(manImageView.transform,M_PI/6.0);

[UIView beginAnimations:@"rotate" context:nil];动画开始

[UIView setAnimationDuration:1];//设置时长

[UIView setAnimationDelegate:self];//设置代理

[manImageView setTransform:transform];//获取transform的值

[UIView commitAnimations];动画结束

//偏移动画

[UIView beginAnimations:@"move" context:nil];动画开始

[UIView setAnimationDuration:2];//设置时长

[UIView setAnimationDelegate:self];//设置代理

manImageView.frame = CGRectMake(100,100,120,100);//改变它的frame的x,y的值

[UIView commitAnimations];动画结束

//翻页动画

[UIView beginAnimations:@"curlUp" context:nil];动画开始

[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];//指定动画曲线类型,该枚举是默认的,线性的是匀速的

[UIView setAnimationDuration:1];//设置时长

[UIView setAnimationDelegate:self];//设置代理

[UIView setAnimationTransition:UIViewAnimationTransitonCurlUp forView:manImageView cache:YES];//设置翻页的方向

[UIView commitAnimations];动画结束

//缩放动画

CGAffineTransform transform;

transform = CGAffineTransformScale(manImageView.transform,1.2,1.2);

[UIView beginAnimations:@"scale" context:nil];动画开始

[UIView setAnimationDuration:2];//设置时长

[UIView setAnimationDelegate:self];//设置代理

[manImageView setTransform:transform];

[UIView commitAnimations];动画结束

//取反的动画效果是根据当前的动画取他的相反的动画

CGAffineTransform transform;

transform = CGAffineTransformInvert(manImageView.transform);

[UIView beginAnimations:@"Invert" context:nil];动画开始

[UIView setAnimationDuration:2];//设置时长

[UIView setAnimationDelegate:self];//设置代理

[manImageView setTransform:transform];//获取改变后的view的transform

[UIView commitAnimations];动画结束

分类:      

CALayer动画 —>UIView动画 —>UIView属性动画、UIViewTransition动画

UIView动画影响的属性:主要是frame视图框架、center视图位置、alpha视图透明度、bounds视图大小、transfrom视图旋转、backgroundColor背景颜色。

+ (void)setAnimationDuration:(NSTimeInterval)duration;动画持续时间

+ (void)setAnimationDelay:(NSTimeInterval)delay;动画开始前延迟

+ (void)setAnimationCurve:(UIViewAnimationCurve)curve;动画的速度曲线

+ (void)setAnimationRepeatAutoreverses:(BOOL)repeatAutoreverses;动画反转

+ (void)setAnimationRepeatCount:(float)repeatCount;动画反转次数

+ (void)setAnimationDelegate:(id)delegate;设置动画的代理

+ (void)setAnimationWillStartSelector:(SEL)selector;动画开始的代理法

+ (void)setAnimationDidStopSelector:(SEL)selector;动画结束的代理法

+ (void)setAnimationBeginsFromCurrentState:(BOOL)fromCurrentState;动画从当前状态继续执行

UIViewBlock动画的用法:

    //第二种动画方式:block(1)

    [UIView animateWithDuration:3 animations:^{

    CGFloat red = arc4random()%256/255.0;

    CGFloat green = arc4random()%256/255.0;

    CGFloat blue = arc4random()%256/255.0;

    UIColor * color = [UIColor colorWithRed:red green:green blue:blue alpha:1];

    self.myView.backgroundColor = color;

    }];

    //block(2)

    [UIView animateKeyframesWithDuration:2 delay:0 options:UIViewKeyframeAnimationOptionCalculationModeLinear animations:^{

    CGFloat red = arc4random()%256/255.0;

    CGFloat green = arc4random()%256/255.0;

    CGFloat blue = arc4random()%256/255.0;

    UIColor * color = [UIColor colorWithRed:red green:green blue:blue alpha:1];

    self.myView.backgroundColor = color;

    } completion:^(BOOL finished) {

        NSLog(@"动画执行完毕后,我再执行...");

    }];

 

//block(3)

    [UIView animateWithDuration:1 delay:0 usingSpringWithDamping:0.1 initialSpringVelocity:1 options:UIViewAnimationOptionLayoutSubviews animations:^{

    CGFloat width = arc4random() %320/1.0;

    CGFloat height = arc4random() %480/1.0;

 

    CGFloat x = arc4random() %(int)(320 - width);

    CGFloat y = arc4random() %(int)(480 - height);

    CGRect frame = CGRectMake(x, y, width, height);

    self.myView.frame = frame;

    } completion:^(BOOL finished) {

        

    }];

*********** 

    //过渡动画

    [UIView transitionWithView:self.myView duration:1 options:UIViewAnimationOptionTransitionFlipFromBottom animations:^{

    CGFloat red = arc4random()%256/255.0;

    CGFloat green = arc4random()%256/255.0;

    CGFloat blue = arc4random()%256/255.0;

    UIColor * color = [UIColor colorWithRed:red green:green blue:blue alpha:1];

    self.myView.backgroundColor = color;

    } completion:^(BOOL finished) {

        

    }];

    

    

    [UIView beginAnimations:@"abc" context:nil];

    [UIView setAnimationDuration:1];

    //平移

    self.myView.transform = CGAffineTransformTranslate(self.myView.transform, 10, 10);

    //旋转

    self.myView.transform = CGAffineTransformRotate(self.myView.transform, M_PI/4.0);

    self.myView.transform = CGAffineTransformMakeRotation(M_PI/4.0);

    [UIView commitAnimations];

    

    

    /*

    CABasicAnimation使用步骤:

     第一步:创建CABasicAnimation对象

     第二步:设置fromValue和toValue

     第三步:将动画添加到layer层上

    */

    

    // CAAnimation 属性动画   CALayer

    //1、CABasicAnimation(第一步)

    CABasicAnimation * animat = [CABasicAnimation animationWithKeyPath:@"backgroundColor"];

 

//    animat.fromValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 100, 100)];

//    animat.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 200, 200)];

    

    //如何将CGColorRef转化成NSValue(第二步)

    animat.fromValue = (id)[UIColor redColor].CGColor;

    animat.toValue = (id)[UIColor greenColor].CGColor;

    

    //(第三步)

    [self.myView.layer addAnimation:animat forKey:@"animate"];

    

    CABasicAnimation * animat1 = [CABasicAnimation animationWithKeyPath:@"borderWidth"];

    animat1.fromValue = [NSNumber numberWithFloat:111];

    animat1.toValue = [NSNumber numberWithFloat:222];

    [self.myView.layer addAnimation:animat1 forKey:@"animate1"];

    

    

    //关键帧动画(分三步)

    CAKeyframeAnimation * anim = [CAKeyframeAnimation animationWithKeyPath:@"backgroundColor"];

    anim.duration = 5;

    anim.repeatCount = 122;

    id color1 = (id)[UIColor redColor].CGColor;

    id color2 = (id)[UIColor greenColor].CGColor;

    id color3 = (id)[UIColor blueColor].CGColor;

    id color4 = (id)[UIColor yellowColor].CGColor;

    id color5 = (id)[UIColor orangeColor].CGColor;

    //设置属性值关键帧

    NSArray * arr = @[color1,color2,color3,color4,color5];

    anim.values = arr;

    //设置时间关键帧

    NSArray * timeArray = @[@0.1,@0.2,@0.3,@0.6,@0.9];

    anim.keyTimes = timeArray;

    //[self.myView.layer addAnimation:anim forKey:@"key"];

 

    //动画组

    CABasicAnimation * basicAnima = [CABasicAnimation animationWithKeyPath:@"bounds"];

    basicAnima.fromValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 50, 20)];

    basicAnima.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 200, 200)];

    CAAnimationGroup * group = [CAAnimationGroup animation];

    group.duration = 5;//时长

    group.repeatCount = 5;//重复次数

    group.animations = @[anim,basicAnima];

    [self.myView.layer addAnimation:group forKey:@"group"];

    

    //过渡动画

    CATransition * anima = [CATransition animation];

    anima.duration = 1;

    anima.repeatCount = 100;

    //type指定动画的类型

    anima.type = @"suckEffect";

    //subtype指定动画方向

    anima.subtype = kCATransitionFromTop;

    [self.myView.layer addAnimation:anima forKey:@"key"];

 

 

 

 

 
posted @ 2015-11-07 12:23  王云鹏  阅读(186)  评论(0)    收藏  举报