浅谈ios开发---动画

闲话少说,直入主题吧还是,在ios开发中的动画实际有三种形式:

1.UIView动画,最基础的动画,主要是改变view的属性来达到动画的效果。

使用这个而动画可以实现如下效果:

1.1淡入淡出(主要改变view的透明度)

1.2位移(主要改变view的位置)

1.3缩放的效果(改变view的缩放比例)

   CGAffineTransform transfarm = self.myView.transform;

  self.myView.transform = CGAffineTransformScale(transfarm, 0.1, 0.1);//缩放比例

下面就给大家演示一下代码(以缩放效果为主,其他效果类似):

  CGAffineTransform transfarm = self.view.transform;

  self.view.transform = CGAffineTransformScale(transfarm, 0.1, 0.1);//动画开始前的缩放比例

    [UIView animateWithDuration:0.2//动画持续时间 animations:^{

        self.view.transform = CGAffineTransformScale(transfarm, 0.8, 0.8);//动画开始的缩放比例

    } completion:^(BOOL finished) {

      [UIView animateWithDuration:0.5 animations:^{

        self.view.transform = CGAffineTransformScale(transfarm, 1, 1);//动画开结束的缩放比例

        }];

    }];

注意:如果动画结束时没有结束动画可以用下边的block来写

[UIView animateWithDuration:0.5 animations:^{

     self.view.transform = CGAffineTransformScale(transfarm, 1, 1);//动画开结束的缩放比例

    }];


下面演示的是父视图上面的两个子视图之间的切换动画效果:

[UIView transitionWithView:self.view duration:0.5 options:UIViewAnimationOptionTransitionFlipFromLeft(切换动画样式) animations:^{

      //视图如何切换的代码(切换动画样式)

     [self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1];//切换父视图上面的两个子视图

    } completion:^(BOOL finished) {

        //结束时候的代码

}];

2.CATransition动画,主要用来两个视图切换过度的动画效果,对UIView的Layer进行控制,系统内置了几种效果,可以直接使用。

#import <QuartzCore/QuartzCore.h>

2.1、父视图上面两个子视图的切换

 CATransition *animation = [CATransition animation];

 animation.duration = 0.6;//设置动画时长

 //加速方式可有以下几种:linear, easeIn, easeOut ,easeInEaseOut ,default

 animation.timingFunction =  [CAMediaTimingFunction functionWithName:@"easeInEaseOut"];

 //动画类型可有以下几种:kCATransitionFade,kCATransitionMoveIn,kCATransitionPush,kCATransitionReveal

 animation.type = kCATransitionFade;

 animation.subtype = kCATransitionFromRight;

 [self.view.layer addAnimation:animation forKey:nil];

 [self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1];

2.2、控制器之间的切换

CATransition *animation = [CATransition animation];

    animation.duration = 0.6;

    animation.timingFunction = [CAMediaTimingFunction functionWithName:@"easeInEaseOut"];

    animation.type = kCATransitionFade;

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

    ViewController *VC = [[ViewController alloc] init];

    [self.navigationController pushViewController:VC animated:YES];

3.CAAnimation,核心动画,结合绘图可以实现灵活多变的动画效果。

在项目中我偶尔会用到CAKeyframeAnimation,下面我就给大家简单介绍一下吧:
它是CApropertyAnimation的子类,跟CABasicAnimation的区别是:CABasicAnimation只能从一个数值(fromValue)变到另一个数值(toValue),可看做是最多只有2个关键帧的CAKeyframeAnimation,而CAKeyframeAnimation会使用一个数组保存这些数值
3.1、CAKeyframeAnimation
 属性解析:
3.1.1、values:就是上述的NSArray对象。里面的元素称为”关键帧”(keyframe)。动画对象会在指定的时间(duration)内,依次显示values数组中的每一个关键帧
3.1.2、path:可以设置一个CGPathRef\CGMutablePathRef,让层跟着路径移动。path只对CALayer的anchorPoint和position起作用。如果你设置了path,那么values将被忽略
3.1.3、keyTimes:可以为对应的关键帧指定对应的时间点,其取值范围为0到1.0,keyTimes中的每一个时间值都对应values中的每一帧.当keyTimes没有设置的时候,各个关键帧的时间是平分的
例如:
Values方式:

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

    

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

    

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

    

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

    

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

    

    NSValue *value5=[NSValue valueWithCGPoint:CGPointMake(100, 100)];


    k.values=@[value1,value2,value3,value4,value5];


    [self.view.layer addAnimation:k forKey:@"SHOW"];

 Path方式:

    CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];

    animation.keyPath = @"position";

    

    CGMutablePathRef path=CGPathCreateMutable();


    CGPathAddEllipseInRect(path, NULL, CGRectMake(150, 100, 100, 100));

    

    animation.path=path;

 

    animation.duration = 4.0f;

    

    [self.view.layer addAnimation:animation forKey:nil];

keyPath可以使用的key

   #define angle2Radian(angle) ((angle)/180.0*M_PI)

transform.rotation.x 围绕x轴翻转 参数:角度 angle2Radian(4)

transform.rotation 默认围绕z轴

transform.scale.x x方向缩放 参数:缩放比例 1.5

transform.scale 所有方向缩放 参数:同上

transform.translation.x x方向移动 参数:x轴上的坐标 100

transform.translation 移动 参数:移动到的点 (100,100)

opacity 透明度 参数:透明度 0.5

backgroundColor 背景颜色 参数:颜色 (id)[[UIColor redColor] CGColor]

cornerRadius 圆角 参数:圆角半径 5

borderWidth 边框宽度 参数:边框宽度 5

bounds 大小 参数:CGRect

contents 内容 参数:CGImage

contentsRect 可视内容 参数:CGRect 值是0~1之间的小数

hidden 是否隐藏

position

shadowColor

shadowOffset

shadowOpacity

shadowRadius







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值