CABasicAnimation学习Demo 包括了一些常用的动画效果

个人写的一些例子:

//
//  ViewController.m
//  CABasicAnimationDemo
//
//  Created by haotian on 14-6-13.
//  Copyright (c) 2014年 Baseus. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize ViewTest;
- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.

    //self.ViewTest.backgroundColor = [UIColor redColor];
    //组合动画调用
    //[self startAnimation2];

    //永久闪烁动画
    //[self opacityForever_Animation:0.3];

    ////有闪烁次数的动画
    //[self opacityTimes_Animation:10 durTimes:0.3];

    //画一条线    路径
    [self drawACurvedLine];

    //路径动画
    //[self animateCicleAlongPath];

}

-(void)startAnimation
{
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
    animation.fromValue = [NSValue valueWithCGPoint:CGPointMake(50, 50)];
    animation.toValue = [NSValue valueWithCGPoint:CGPointMake(300, 300)];
    animation.duration = 3.0f;
    animation.repeatCount = 1;
    //animation.removedOnCompletion = NO; //完成后是否回到原来状态,如果为NO 就是停留在动画结束时的状态
    //animation.fillMode = kCAFillModeRemoved;//动画完成后返回到原来状态
    //animation.fillMode = kCAFillModeBackwards;
    animation.fillMode = kCAFillModeForwards;//当动画完成时,保留在动画结束的状态

    [self.ViewTest.layer addAnimation:animation forKey:nil];
}

-(void)startAnimation1
{
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
    animation.fromValue = [NSNumber numberWithFloat:0.0f];
    animation.toValue   = [NSNumber numberWithFloat:10.0f];
    //animation.duration = 0.5f;
    //animation.fillMode = kCAFillModeForwards;
    //animation.removedOnCompletion = NO;
    //animation.repeatCount = 2;
    //[self.ViewTest.layer addAnimation:animation forKey:nil];

    CABasicAnimation *animation1 = [CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
    animation.fromValue = [NSNumber numberWithFloat:0.0f];
    animation.toValue   = [NSNumber numberWithFloat:10.0f];
    //animation.duration = 0.5f;
    //animation.fillMode = kCAFillModeForwards;
    //animation.removedOnCompletion = NO;
    //animation.repeatCount = 2;
    //[self.ViewTest.layer addAnimation:animation1 forKey:nil];

    CAAnimationGroup *groupAnimation = [CAAnimationGroup animation];
    groupAnimation.duration = 2.0f;
    groupAnimation.autoreverses  = YES;
    groupAnimation.repeatCount = 5;
    [groupAnimation setAnimations:[NSArray arrayWithObjects:animation,animation1, nil]];

    [self.ViewTest.layer addAnimation:groupAnimation forKey:nil];
}

//组合动画
-(void)startAnimation2
{
    //界限
    CABasicAnimation *boundsAnimation = [CABasicAnimation animationWithKeyPath:@"bounds"];
    boundsAnimation.fromValue = [NSValue valueWithCGRect: self.ViewTest.bounds];
    boundsAnimation.toValue = [NSValue valueWithCGRect:CGRectZero];
    //透明度变化
    CABasicAnimation *opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
    opacityAnimation.fromValue = [NSNumber numberWithFloat:1.0];
    opacityAnimation.toValue = [NSNumber numberWithFloat:0.5];

    //位置移动
    CABasicAnimation *animation  = [CABasicAnimation animationWithKeyPath:@"position"];
    animation.fromValue =  [NSValue valueWithCGPoint: self.ViewTest.layer.position];
    CGPoint toPoint = self.ViewTest.layer.position;
    toPoint.x += 180;
    animation.toValue = [NSValue valueWithCGPoint:toPoint];

    //旋转动画
    CABasicAnimation* rotationAnimation =
    [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];//"z"还可以是“x”“y”,表示沿z轴旋转

    rotationAnimation.toValue = [NSNumber numberWithFloat:(2 * M_PI) * 3];
    // 3 is the number of 360 degree rotations

    // Make the rotation animation duration slightly less than the other animations to give it the feel
    // that it pauses at its largest scale value
    rotationAnimation.duration = 3.0f;
    rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; //缓入缓出
    //rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];

    //缩放动画
    CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    scaleAnimation.fromValue = [NSNumber numberWithFloat:0.0];
    scaleAnimation.toValue = [NSNumber numberWithFloat:1.0];
    scaleAnimation.duration = 3.0f;
    scaleAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    //组合动画
    CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
    animationGroup.duration = 3.0f;
    animationGroup.autoreverses = YES;   //是否重播,原动画的倒播
    animationGroup.repeatCount = NSNotFound;//HUGE_VALF;     //HUGE_VALF,源自math.h
    [animationGroup setAnimations:[NSArray arrayWithObjects:rotationAnimation, scaleAnimation,boundsAnimation, nil]];

    //将上述两个动画编组

    [self.ViewTest.layer addAnimation:animationGroup forKey:@"animationGroup"];
}

//永久闪烁的动画
-(void)opacityForever_Animation:(float)time
{
    CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"opacity"];
    animation.fromValue=[NSNumber numberWithFloat:1.0];
    animation.toValue=[NSNumber numberWithFloat:0.0];
    animation.autoreverses=YES;
    animation.duration=time;
    animation.repeatCount=FLT_MAX;
    animation.removedOnCompletion=NO;
    animation.fillMode=kCAFillModeForwards;

    [self.ViewTest.layer addAnimation:animation forKey:@"opacityForever"];
}
/**************************************************************************/

//有闪烁次数的动画

-(void)opacityTimes_Animation:(float)repeatTimes durTimes:(float)time;
{
    CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"opacity"];
    animation.fromValue=[NSNumber numberWithFloat:1.0];
    animation.toValue=[NSNumber numberWithFloat:0.4];
    animation.repeatCount=repeatTimes;
    animation.duration=time;
    animation.removedOnCompletion=NO;
    animation.fillMode=kCAFillModeForwards;
    animation.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
    animation.autoreverses=YES;

    [self.ViewTest.layer addAnimation:animation forKey:@"opacityTimes"];

}
/**************************************************************************/
//路径动画

-(void)keyframeAniamtion:(CGMutablePathRef)path durTimes:(float)time Rep:(float)repeatTimes
{

}

//在视图中画一条线
-(void)drawACurvedLine
{
    UIGraphicsBeginImageContext(CGSizeMake(320, 460));
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetLineWidth(context, 3);
    CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
    //设置起点
    CGContextMoveToPoint(context, 10, 10);

    CGContextAddQuadCurveToPoint(context, 10, 450, 310, 450);
    //CGContextAddQuadCurveToPoint(context, 310, 10, 10, 10);

    //划线
    CGContextDrawPath(context, kCGPathStroke);

    //得到一个image 从目前的矢量上下文
    UIImage *curve = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    UIImageView *curveView = [[UIImageView alloc]initWithImage:curve];
    curveView.frame = CGRectMake(1, 1, 320, 460);
    [curveView setBackgroundColor:[UIColor clearColor]];
    [self.view addSubview:curveView];
}

-(void)animateCicleAlongPath
{
    //准备关键帧动画
    CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    pathAnimation.calculationMode = kCAAnimationPaced;
    pathAnimation.fillMode = kCAFillModeForwards;
    pathAnimation.removedOnCompletion = NO;
    pathAnimation.duration = 5.0f;
    pathAnimation.repeatCount = 200;
    //pathAnimation.autoreverses = YES; //原路返回,而不是从头再来
    //设置动画路径
    //CGPoint endPoint = CGPointMake(310, 450);
    CGMutablePathRef curvedPath = CGPathCreateMutable();
    CGPathMoveToPoint(curvedPath, NULL, 10, 10);
    CGPathAddQuadCurveToPoint(curvedPath, NULL, 10, 450, 310, 450);
    CGPathAddQuadCurveToPoint(curvedPath, NULL, 310, 10, 10, 10);
    //已经有了路径,我们要告诉动画  路径
    pathAnimation.path = curvedPath;
    CGPathRelease(curvedPath);  //这里要手工释放

    [self.ViewTest.layer addAnimation:pathAnimation forKey:nil];

}
/**************************************************************************/

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

最后附上Demo下载地址:http://download.csdn.net/detail/u012951123/7500419

CABasicAnimation学习Demo 包括了一些常用的动画效果,布布扣,bubuko.com

时间: 2024-10-09 09:35:22

CABasicAnimation学习Demo 包括了一些常用的动画效果的相关文章

CABasicAnimation学习Demo 包含了一些经常使用的动画效果

个人写的一些样例: // // ViewController.m // CABasicAnimationDemo // // Created by haotian on 14-6-13. // Copyright (c) 2014年 Baseus. All rights reserved. // #import "ViewController.h" @interface ViewController () @end @implementation ViewController @syn

Stage3D学习笔记(六):旋转动画效果

我们这节在上一篇代码的基础上再进一步,让显示的纹理进行旋转. 实现旋转,只需要每帧修改_modelViewMatrix的旋转角度即可,我们需要一个变量来记录旋转: 1 //旋转度数 2 private var _rotation:Number = 0; 每帧修改后的数据需要重新提交到GPU,而已经提交的数据不改动则不需要重新进行提交,所以我们修改一下render方法即可: 1 private function render(event:Event):void 2 { 3 //没有变动的数据就不需

slidingmenu+fragment实现常用的侧滑效果(包括Fragment状态的保存)

一.需求 关于fragment的问题,一直想写一篇博客了,应该当初自己也是对这玩意一点都不熟悉到现在也大概知道个日常的使用的地步. 一个侧滑的导航栏,内有4个条目,每个选项点击进入对应的界面,每一个界面是一个fragment,各界面之间自由切换,且可以保存之前的状态,也就是说,切换的过程并不会产生新的对象,不会重新去new 一个fragment对象,不需要每次点击重新加载数据,这里就涉及了一个很重要的问题,fragment状态的保存,在这篇文章里,我尽量用实例把这个问题说清楚,毕竟当初也是查了不

Cocos2D学习笔记(1)- 常用的类

1.坐标系 >屏幕坐标系(UIKit):原点在左上角! >OpenGl坐标系:原点在屏幕的左下角! 2.游戏设计:Director--Scene--Layer--Sprite. >CCDirector:导演类,相当于是游戏策划,负责整个游戏的布局和运行规则的制定. >CCScene:场景类,每个场景可以是一个界面或一个关卡. >CCLayer:图层类,为了方便游戏界面的渲染管理. >CCSprite:精灵类, 小结:一个导演类(CCDirector)可以指挥多个场景类(

ios7开发学习笔记-包括c oc 和ios介绍

请查看我的新浪资料分享 http://iask.sina.com.cn/u/2430843520 ios7开发学习笔记-包括c oc 和ios介绍,布布扣,bubuko.com

SecureCRT学习之道:SecureCRT常用快捷键设置与字体设置方法

常用快捷键: 全屏View->Full Screen 快捷键 Alt + Enter 菜单View 快捷键 Alt + V 打开新的终端 快捷键 Alt + B 字体设置: options->global options->general->Default session-> Edit Defalut Settings 网上有人建议用fixedsys,这个字体虽然不是最美丽的,但在终端上也是一个不错选择,大家都可以接受. 选择它,主要是因为它大小固定,在所有地方显示,效果都一

SecureCRT学习之道:SecureCRT 常用技巧

快捷键: 1. ctrl + a :  移动光标到行首 2. ctrl + e :移动光标到行尾 3. ctrl + d :删除光标之后的一个字符 4. ctrl + w : 删除行首到当前光标所在位置的所有字符 5. crtl + k : 删除当前光标到行尾的所有字符 6. alt + b : 打开快速启动栏 7. alt + 1/2/3... : 在多个不同的session标签之间切换 鼠标复制: options -> global options ->  Terminal  钩上Copy

Linux学习笔记一 磁盘管理常用命令

Linux学习笔记一 磁盘管理常用命令 Linux系统下,一切均是文件,磁盘是一种特殊的块设备文件. 常用的硬盘接口类型 并口:IDE ,SCSI 串口:SATA,SAS 磁盘设备文件设备文件的命令规则: IDE: 设备名为/dev/hda, /dev/hdb-.  (末尾的a ,b-代表不同的IDE硬盘) SCSI:设备名为/dev/sda, /dev/sdb-.(末尾的a,b-代表不用的SCSI硬盘 机械式硬盘: track: 磁道(盘面上由外向里划分成不同的磁道) cylinder: 柱面

[学习总结]3、Android---Scroller类(左右滑动效果常用的类)

参考资料:http://blog.csdn.net/vipzjyno1/article/details/24592591 非常感谢这个兄弟! 在android学习中,动作交互是软件中重要的一部分,其中的Scroller就是提供了拖动效果的类,在网上,比如说一些Launcher实现滑屏都可以通过这个类去实现.. 先来看一下demo的效果:  下载地址 学习Scroller这个类之前你需要学习一下下面的知识: 1.Scroller的2个方法scrollTo()和scrollBy()不了解的点这里 s