动画基础

        所有核心动画的动画类都是从 CAAnimation类继承而来


        CAAnimation 实现了 CAMediaTiming协议,提供了动画的持续时间,速度,和重复计数


        CAAnimation 也实现了 CAAction协议。该协议为图层触发一个动画动作提供了提供 标准化响应


        CATransition 提供了一个图层变化的过渡效果,它能影响图层的整个内容。 动画进行的时候淡入淡出(fade)、推(push)、显露(reveal)图层的内容。这些过渡效果可以扩展到你自己定制的Core Image 滤镜


        CAAnimationGroup 允许一系列动画效果组合在一起,并行显示动画


        CAPropertyAnimation是一个抽象的子类,它支持动画的显示图层的关键路径中指定的属性


        CABasicAnimation 简单的为图层的属性提供修改。


        CAKeyframeAnimation支持关键帧动画,你可以指定的图层属性的关键路径动画,包括动画的每个阶段的价值,以及关键帧时间和计时功能的一系列值。在 动画运行是,每个值被特定的插入值替代



        核心动画的 CAConstraint 类 是一个布局管理器,它可以指定子图层类限制于你指定的约束集合。每个约束(CAConstraint类的实例封装)描述层的几何属性(左,右,顶部或底部的边缘或水平或垂直中心)的关系,关系到其同级之一的几何属性层或superlayer


        核心动画的图层和 Cocoa 的视图在很大程度上没有一定的相似性,但是他们两者最大的区别是,图层不会直接渲染到屏幕上


        试图的position坐标和anchorPoint有关


        sublayerTransform属性指定的矩阵只会影响图层的子图层,而不会对 图层本身产生影响


        transform 属性指定的矩阵结合图层的anchorPoint属性作用于图层和图层的子图层上 面


        旋转的单位采用弧度(radians),而不是角度(degress)。以下两个函数,你可以在 弧度和角度之间切换


CGFloat DegreesToRadians(CGFloat degrees) {returndegrees * M_PI / 180;};

CGFloat RadiansToDegrees(CGFloat radians) {returnradians * 180 / M_PI;};



无法正常运行:

        替换的办法是,你必须通过 setValue:forKeyPath:或者valueForKeyPath:方法,具体如下:


myLayer.transform.rotation.x=0;

[myLayer setValue:[NSNumber numberWithInt:0]forKeyPath:@"transform.rotation.x"];


        如果一个图层的属性 needsDisplayOnBoundsChange被设置为YES 的时候,当图层的bounds属性改变的时候,图层的内容将会被重新缓存起来。默认情况下图层的needsDisplayOnBoundsChange属性值为NO。


给CALayer提供内容

        1。包含图片内容的 CGImageRef来显式的设置图层的contents 的属性。

        2。指定一个委托,它提供或者重绘内容。

        3。继承 CALayer 类重载显示的函数


创建一个委托类实

        displayLayer:或 drawLayer:inContext:


- (void)displayLayer:(CALayer *)theLayer

{

    // check the value of the layer's state key

    if ([[theLayer valueForKey:@"state"] boolValue])

    {

        // display the yes image

        theLayer.contents=[someHelperObjectloadStateYesImage];

    }

    else {

        // display the no image

        theLayer.contents=[someHelperObjectloadStateNoImage];

    }

}



        如果你必须重绘图层的内容,而不是通过加载图片,那你需要实现drawLayer:inContext:方法。通过委托可以决定哪些内容是需要的并使用CGContextRef来重绘内容。


        实例重新缓存其内容,可以通过发送以下任何一个方法 setNeedsDisplay或setNeedsDisplayInRect:的消息,亦或者设置图层的needsDisplaOnBoundsChange属性为 YES


        CALayer 的属性 contentsGravity允许你在图层的边界内容修改图层的contents图片的位置或者伸缩值。默认情况下,内容的图像完全填充层的边界,忽视自然的图像 宽高比 contentsGravity属性值:


        CABasicAnimation提供了在图层的属性值间简单的插入。 CAKeyframeAnimation提供支持关键帧动画。你指定动画的一个图层属性的关键路径,一个表示在动画的每个阶段的价值的数组,还有一个关键帧时间的数组和时间函数。


        CATransition提供了一个影响整个图层的内容过渡效果。在动画显示过程中采用淡出(fade)、推出(push)、显露(reveal)图层的内容。常用的过渡效果可以通过提供你自己定制的核心图像滤镜来扩展


CABasicAnimation *_basicAnimation= [CABasicAnimation animationWithKeyPath:@"opacity"];

   _basicAnimation.duration=3.0;

   _basicAnimation.repeatCount=2;

   _basicAnimation.autoreverses=YES;

   _basicAnimation.fromValue=[NSNumber numberWithFloat:1.0];

   _basicAnimation.toValue=[NSNumber numberWithFloat:0.0];

    [self.m_imageViewBg.layeraddAnimation:_basicAnimation forKey:@"animateOpacity"];



我们可以通过animationWithKeyPath键值对的方式来改变动画

        animationWithKeyPath的值:

        transform.scale= 比例轉換

        transform.scale.x =闊的比例轉換

        transform.scale.y =高的比例轉換

        transform.rotation.z =平面圖的旋轉

        opacity = 透明度

        margin

        zPosition

        backgroundColor

        cornerRadius

        borderWidth

        bounds

        contents

        contentsRect

        cornerRadius

        frame

        hidden

        mask

        masksToBounds

        opacity

        position

        shadowColor

        shadowOffset

        shadowOpacity

        shadowRadius



[self. ui_View.layerremoveAllAnimations];

   

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

   pulse.timingFunction =[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];

   pulse.duration = 0.5 + (rand() % 10) * 0.05;

   pulse.repeatCount = 1;

   pulse.autoreverses = YES;

   pulse.fromValue =[NSNumber numberWithFloat:.8];

   pulse.toValue =[NSNumber numberWithFloat:1.2];

   [self.ui_View.layer addAnimation:pulseforKey:nil];

// bounds

 

CABasicAnimation *anim = [CABasicAnimationanimationWithKeyPath:@"bounds"];

   anim.duration = 1.f;

   anim.fromValue =[NSValue valueWithCGRect:CGRectMake(0,0,10,10)];

   anim.toValue =[NSValue valueWithCGRect:CGRectMake(10,10,200,200)];

   anim.byValue  = [NSValuevalueWithCGRect:self. ui_View.bounds]; 

//    anim.toValue= (id)[UIColor redColor].CGColor;

//   anim.fromValue =  (id)[UIColorblackColor].CGColor;

   

   anim.timingFunction =[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

   anim.repeatCount = 1;

   anim.autoreverses = YES;

   

   [ui_View.layeraddAnimation:anim forKey:nil];

//cornerRadius

 

   CABasicAnimation*anim2 = [CABasicAnimationanimationWithKeyPath:@"cornerRadius"];

   anim2.duration = 1.f;

   anim2.fromValue =[NSNumber numberWithFloat:0.f];

   anim2.toValue =[NSNumber numberWithFloat:20.f];

   anim2.timingFunction =[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

   anim2.repeatCount = CGFLOAT_MAX;

   anim2.autoreverses = YES;

   

   [ui_View.layeraddAnimation:anim2 forKey:@"cornerRadius"];

//contents

 

CABasicAnimation *anim = [CABasicAnimationanimationWithKeyPath:@"contents"];

   anim.duration = 1.f;

   anim.fromValue =(id)[UIImageimageNamed:@"1.jpg"].CGImage;

   anim.toValue =(id)[UIImageimageNamed:@"2.png"].CGImage;

//   anim.byValue  = (id)[UIImageimageNamed:@"3.png"].CGImage;

//    anim.toValue= (id)[UIColor redColor].CGColor;

//   anim.fromValue =  (id)[UIColorblackColor].CGColor;

   

   anim.timingFunction =[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

   anim.repeatCount = CGFLOAT_MAX;

   anim.autoreverses = YES;

   

   [ui_View.layeraddAnimation:anim forKey:nil];

 

[ui_View.layersetShadowOffset:CGSizeMake(2,2)];

   [ui_View.layersetShadowOpacity:1];

   [ui_View.layersetShadowColor:[UIColorgrayColor].CGColor];

//    

   CABasicAnimation *anim =[CABasicAnimation animationWithKeyPath:@"shadowColor"];

   anim.duration = 1.f;

   anim.toValue =(id)[UIColorredColor].CGColor;

   anim.fromValue =  (id)[UIColorblackColor].CGColor;

   

   anim.timingFunction =[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

   anim.repeatCount = CGFLOAT_MAX;

   anim.autoreverses = YES;

   

   [ui_View.layeraddAnimation:anim forKey:nil];

   

   CABasicAnimation *_anim= [CABasicAnimation animationWithKeyPath:@"shadowOffset"];

   _anim.duration = 1.f;

   _anim.fromValue =[NSValue valueWithCGSize:CGSizeMake(0,0)];

   _anim.toValue =[NSValue valueWithCGSize:CGSizeMake(3,3)];

   

   _anim.timingFunction =[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

   _anim.repeatCount = CGFLOAT_MAX;

   _anim.autoreverses = YES;

   

   [ui_View.layeraddAnimation:_anim forKey:nil];

   

   

   CABasicAnimation *_anim1= [CABasicAnimation animationWithKeyPath:@"shadowOpacity"];

   _anim1.duration = 1.f;

   _anim1.fromValue =[NSNumber numberWithFloat:0.5];

   _anim1.toValue =[NSNumber numberWithFloat:1];

   

   _anim1.timingFunction =[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

   _anim1.repeatCount = CGFLOAT_MAX;

   _anim1.autoreverses = YES;

   

   [ui_View.layeraddAnimation:_anim1 forKey:nil];

   

   

   

   CABasicAnimation *_anim2= [CABasicAnimation animationWithKeyPath:@"shadowRadius"];

   _anim2.duration = 1.f;

   _anim2.fromValue =[NSNumber numberWithFloat:10];

   _anim2.toValue =[NSNumber numberWithFloat:5];

   

   _anim2.timingFunction =[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

   _anim2.repeatCount = CGFLOAT_MAX;

   _anim2.autoreverses = YES;

   

   [ui_View.layeraddAnimation:_anim2 forKey:nil];



        CAScrollLayer 对象的滚动区域的范围在它的子图层里面定义。


        CAScrollLaye 不提供键盘或鼠标事件处理,也不提供可见的滚动条。


        CATextLayer可以方便的从字符串或字符串的内容创建一个图层类的内容


        CATiledLayer 允许递增的显示大而复杂的图片


        CAEAGLLayer 提供了一个OpenGLES渲染环境


        CALayer 的还扩展了 NSKeyValueCoding的非正式协议,加入默认键值和额外的结构类型的自动对象包装(CGPoint,CGSize,CGRect,CGAffineTransform和 CATransform3D)的支持,并提供许多这些结构的关键路径领域的访问



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值