代码改变世界

窗口的简单动画

2012-03-21 13:28  java环境变量  阅读(353)  评论(0编辑  收藏  举报

刚刚看到一个窗口动画,给弄个简单例子!
创建一个基于view-based的工程,然后添加头文件#import <QuartzCore/QuartzCore.h>和其frameworks
然后修改代码!
代码实例如下:

- (void)viewDidLoad {

    [super viewDidLoad];
       //创建一个内框图
        CGRect myImageRect = CGRectMake(0.0f, 0.0f, 320.0f, 200.0f);
        UIView *myView = [[UIView alloc] initWithFrame:myImageRect];
        myView.backgroundColor = [UIColor redColor];


        CABasicAnimation *theAnimation1;    //定义动画
        theAnimation1=[CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
        theAnimation1.duration=5;//动画持续时间
        theAnimation1.repeatCount=3;//动画重复次数
        theAnimation1.autoreverses=YES;//是否自动重复
        theAnimation1.fromValue=[NSNumber numberWithFloat:0];
        theAnimation1.toValue=[NSNumber numberWithFloat:-100];
        [myView.layer addAnimation:theAnimation1 forKey:@"animateLayer"];
        
        [self.view addSubview:myView];
//左右摇动效果实现
        CABasicAnimation *theAnimation;    
        theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
        theAnimation.duration=1;
        theAnimation.repeatCount=10;
        theAnimation.autoreverses=YES;
        theAnimation.fromValue=[NSNumber numberWithFloat:0];
        theAnimation.toValue=[NSNumber numberWithFloat:-60];
        [self.view.layer addAnimation:theAnimation forKey:@"animateLayer"];
        [myView release];
}