现在的位置: 首页 > 综合 > 正文

calayer动画总结(一)

2018年05月12日 ⁄ 综合 ⁄ 共 4112字 ⁄ 字号 评论关闭


核心动画提供了一套你可以在你的应用程序里面使用的动画类的表现:

第一、CABasicAnimation提供了在图层的属性值间简单的插入;

第二、CAKeryframeAnimation提供支持关键帧动画。你指定动画的一个图层属性的关键路径,一个表示在动画的每个阶段的数组,还有一个关键帧时间的数组和时间的函数;

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

除了要指定显示的动画类型,你还必须指定动画的间隔、它的速度(它的插值如何分布在整个动画过程)、动画循环时候的循环次数、动画周期完成的时候是否自动的反转、还有动画结束的时候它的可视化状态。动画类和CAMediaTiming协议提供所有这些功能甚至更多的功能。

一、CALayer

由于核心动画集成到了 cocoa 中,因而你通过给窗口、视图、层简单的设定一些你感兴趣的属性值, 从而使那些可视的组件做动画到新的值。当你使用 CALayer 的时候,你需要做的就是直接设置这些值。例如, 如果你想改变一个层的边框大小,你简单的调用[layer setBounds:newFrame],这里要说的是,层是你已经创建 的 CALayer 对象,并且要增加这个对象到层树中,而 newFrame 是一个 CGRect 包含了边框的尺寸和原点。当 这些代码运行时,就会使用关键路径“bounds”的默认动画,使层具有边框大小改变时的动画。

简单的说,当你使用一个窗口(NSWindow)或者视图(NSView),你需要做的就是,使用了动画的代理对象, 来设置窗口或者视图的属性值。这意味着,例如,你可以代替使用[view setFrame:newFrame]来设置窗口的框架, 通过[[view animtor] setFrame:newFrame]这个函数调用,达到目的。不同之处就是,你用了一个动画的代理对象, 设置了这些属性,而这些都会隐式的执行框架的初始值到结束值的动画。

 

1、

UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 60, 320, 100)];

    imageView.userInteractionEnabled=YES;

    [self.view addSubview:imageView];

// Do any additional setup after loading the view.

    NSString *path=[[NSBundle mainBundle]pathForResource:@"balloon" ofType:@"jpg"];

    UIImage *image=[[UIImage alloc]initWithContentsOfFile:path];

    layer=[CALayer layer];

    

    x=0;

    y=0;

    width=320;

    height=300;

    

    [layer setFrame:CGRectMake(x, y, width, height)];

    // Set the layer contents to our baloon image.

    [layer setContents:(id)[self nsImageToCGImageRef:image]];

    // Add our layer to the sublayers at index 0 so that is displays

    // behind the controls we added

    [[imageView layer]insertSublayer:layer atIndex:0];

    [image release];

二、CABasicAnimation

你已经发现了,所有可以在层上做动画的属性。然而,在动画对象(CABasicAnimation)中还有许多 有用的属性,这些属性可以让你便于控制动画,提升动画的性能。

Autoreverses

当你设定这个属性为 YES 时,在它到达目的地之后,动画的属性会返回到开始的值,代替了直接跳转到 开始的值。

DurationDuration 这个参数你已经相当熟悉了。它设定开始值到结束值花费的时间。期间会被速度的属性所影响。 RemovedOnCompletion这个属性默认为 YES,那意味着,在指定的时间段完成后,动画就自动的从层上移除了。这个一般不用。

假如你想要再次用这个动画时,你需要设定这个属性为 NO。这样的话,下次你在通过-set 方法设定动画的属 性时,它将再次使用你的动画,而非默认的动画。

Speed

默认的值为 1.0.这意味着动画播放按照默认的速度。如果你改变这个值为 2.0,动画会用 2 倍的速度播放。 这样的影响就是使持续时间减半。如果你指定的持续时间为 6 秒,速度为 2.0,动画就会播放 3 秒钟---一半的 持续时间。

BeginTime

这个属性在组动画中很有用。它根据父动画组的持续时间,指定了开始播放动画的时间。默认的是 0.0.组 动画在下个段落中讨论“Animation Grouping”。

TimeOffset

如果一个时间偏移量是被设定,动画不会真正的可见,直到根据父动画组中的执行时间得到的时间都流逝 了。

RepeatCount

默认的是 0,意味着动画只会播放一次。如果指定一个无限大的重复次数,使用 1e100f。这个不应该和 repeatDration 属性一块使用。

RepeatDuration

这个属性指定了动画应该被重复多久。动画会一直重复,直到设定的时间流逝完。它不应该和 repeatCount 一起使用。

 

2、

CGPoint startPoint=CGPointMake(280, 300);

    CGPoint endPoint=CGPointMake(30, 50);

  CABasicAnimation *animation =

    [CABasicAnimation animationWithKeyPath:@"position"];

        [animation setFromValue:[NSValue valueWithCGPoint:startPoint]];

        [animation setToValue:[NSValue valueWithCGPoint:endPoint]];

        [animation setDuration:5.0];

        [animation setFillMode:kCAFillModeForwards];

        [[button1 layer] setPosition:endPoint];        

        [[button1 layer]addAnimation:animation forKey:@"button"];

3、CAAnimationGroup

“有用的动画属性”,我们定义了 2 个特殊的属性,是关系到动画组的:beginTime 和

timeOffset。

CGRect oldRect=CGRectMake(0, 0, 100, 100);

    CGRect newRect=CGRectMake(0, 0, 300, 300);

    

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

    [boundsAnimation setFromValue:[NSValue valueWithCGRect:oldRect]];

    [boundsAnimation setToValue:[NSValue valueWithCGRect:newRect]];

    [boundsAnimation setDuration:15.0f];

    [boundsAnimation setBeginTime:0.0f];

    

    CABasicAnimation *positionAnimation=[CABasicAnimation animationWithKeyPath:@"position"];

    [positionAnimation setFromValue:[NSValue valueWithCGRect:[view2 layer].frame]];

    [positionAnimation setToValue:[NSValue valueWithCGPoint:CGPointMake(0, 0)]];

    [positionAnimation setDuration:15.0f];

    [positionAnimation setBeginTime:5.0f];

    

    CABasicAnimation *borderWidthAnimation =

    [CABasicAnimation animationWithKeyPath:@"borderWidth"];

    [borderWidthAnimation setFromValue:[NSNumber numberWithFloat:5.0f]];

    [borderWidthAnimation setToValue:[NSNumber numberWithFloat:30.0f]];

    [borderWidthAnimation setDuration:15.0f];

    [borderWidthAnimation setBeginTime:10.0f];

    

    CAAnimationGroup *group=[CAAnimationGroup animation];

    [group setDuration:15];

    [group setAnimations:[NSArray arrayWithObjects:boundsAnimation,positionAnimation,borderWidthAnimation, nil]];

    [[view2 layer]addAnimation:group forKey:nil];

   

【上篇】
【下篇】

抱歉!评论已关闭.