CAAnimation 类基本学习

来源:互联网 发布:linux调用内核模块 编辑:程序博客网 时间:2024/06/12 01:42

CAAnimation类 提供的更多的动画效果,比UIView更方便,更好用,这个类也是一个抽象类,有sange个类 CAPropertyAnimation(CABasicAnimationCAKeyframeAnimation) 也是抽象类 括号中是它的两个子类 CAAnimationGroup

or CATransition. 所以使用CAAnimation提供动画效果 需要这四个子类

以下是一些代码实现与解释


- (void)viewDidLoad {

    [superviewDidLoad];

    [superviewDidLoad];

   UIView *view = [[UIViewalloc] initWithFrame:CGRectMake(40,40, 100, 100)];

    [view setBackgroundColor:[UIColorgreenColor]];

    [viewsetTag:1001];

    [view.layersetShadowColor:[UIColorredColor].CGColor];

    [view.layersetShadowOffset:CGSizeMake(-10, -10)];

    [view.layersetShadowOpacity:1.0];

    [view.layersetShadowRadius:7.0];

    

    [self.viewaddSubview:view];

    

    

    //建立的三个button

   UIButton *basicButton = [selfbuttonWithCGRect:CGRectMake(20,300, 80, 30) ttitle:@"basic"backgraoudColor:[UIColorredColor] Target:selfAction:@selector(dobasicAnimation:)];

    [self.viewaddSubview:basicButton];

    

   UIButton *keyFrameButton = [selfbuttonWithCGRect:CGRectMake(120,300, 80, 30) ttitle:@"keyFrame"backgraoudColor:[UIColorredColor] Target:selfAction:@selector(dokerFramAnimation:)];

    [self.viewaddSubview:keyFrameButton];

    

   UIButton *transitionButton = [selfbuttonWithCGRect:CGRectMake(240,300, 80, 30) ttitle:@"trans"backgraoudColor:[UIColorredColor] Target:selfAction:@selector(doTransAnimation:)];

    [self.viewaddSubview:transitionButton];

    

    

    // Do any additional setup after loading the view, typically from a nib.

}


- (void)dobasicAnimation:(UIButton *)button

{

    // keyPath CALayer 可以做动画的效果

   UIView *view = [self.viewviewWithTag:1001];

    CABasicAnimation *basic = [CABasicAnimationanimationWithKeyPath:@"position.x"];


    //在x轴平移 

    basic.fromValue = [NSNumbernumberWithFloat:100];

    basic.toValue = [NSNumbernumberWithFloat:250];

    basic.duration =3;

    //basic.fromValue = (id)[UIColor redColor].CGColor;

   // basic.toValue = (id)[UIColor yellowColor].CGColor;

    

  //  [view.layer addAnimation:basic forKey:nil];

//改变颜色

    CABasicAnimation *basic1 = [CABasicAnimationanimationWithKeyPath:@"backgroundColor"];

    basic1.fromValue = (id)[UIColorredColor].CGColor;

    basic1.toValue = (id)[UIColoryellowColor].CGColor;

    basic1.duration =3;

    

//把一组动画加入到一起,同时执行, animations属性接受一个数组,数组中存放的就是每一组动作

    CAAnimationGroup *group = [CAAnimationGroupanimation];

   NSArray * array = [NSArrayarrayWithObjects:basic,basic1,nil];

    group.animations = array;

    group.duration =3;

    [view.layeraddAnimation:group forKey:nil];

    

}


- (void)dokerFramAnimation:(UIButton *)button

{

   UIView *view = [self.viewviewWithTag:1001];

    CAKeyframeAnimation *key = [CAKeyframeAnimationanimationWithKeyPath:@"position"];


//连续移动

//    key.values = [NSArray arrayWithObjects:

//                  [NSValue valueWithCGPoint:CGPointMake(40, 40)],

//                  [NSValue valueWithCGPoint:CGPointMake(400, 150)],

//                  [NSValue valueWithCGPoint:CGPointMake(140, 210)],

//                  [NSValue valueWithCGPoint:CGPointMake(320, 160)],

//                  [NSValue valueWithCGPoint:CGPointMake(60, 80)],

//                  nil];

//    key.keyTimes = [NSArray arrayWithObjects:

//                    [NSNumber numberWithFloat:0.1],

//                    [NSNumber numberWithFloat:0.2],

//                    [NSNumber numberWithFloat:0.3],

//                    [NSNumber numberWithFloat:0.6],

//                    [NSNumber numberWithFloat:0.3],

//                    [NSNumber numberWithFloat:0.6],

//                    nil];

    //制作晃动效果

    key.values = [NSArrayarrayWithObjects:

                  [NSValuevalueWithCGPoint:CGPointMake(view.center.x-5, view.center.y)],

                  [NSValuevalueWithCGPoint:CGPointMake(view.center.x, view.center.y)],

                  [NSValuevalueWithCGPoint:CGPointMake(view.center.x+5, view.center.y)],

                  [NSValuevalueWithCGPoint:CGPointMake(view.center.x, view.center.y)],

                  [NSValuevalueWithCGPoint:CGPointMake(view.center.x-5, view.center.y)],

                  [NSValuevalueWithCGPoint:CGPointMake(view.center.x, view.center.y)],

                  [NSValuevalueWithCGPoint:CGPointMake(view.center.x+5, view.center.y)],

                 nil];

   // key.duration = 2;

    [view.layeraddAnimation:key forKey:nil];

    

}


- (void)doTransAnimation:(UIButton *)button

{

    //CAAnimation;

   UIView *view = [self.viewviewWithTag:1001];

    CATransition *trans = [CATransitionanimation];

//设置类型,枚举可以有从左, 右, 上, 下四个方向

    [trans setType:kCATransitionMoveIn];

    trans.subtype =kCATransitionFromBottom;

//设置一个代理,结束或开始时,实现代理中提供的方法,或者操作。

    trans.delegate =self;

    [view.layeraddAnimation:trans forKey:nil];

}



//一个创建button的函数

- (UIButton *) buttonWithCGRect:(CGRect)rect ttitle:(NSString *)title backgraoudColor:(UIColor *)color Target:(id)target Action:(SEL)action

{

    UIButton *button = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

    button.frame = rect;

    [button setTitle:titleforState:UIControlStateNormal];

    [buttonsetBackgroundColor:color];

    //size of world on button

    [button.titleLabelsetFont:[UIFontsystemFontOfSize:16]];

    [button addTarget:targetaction:action forControlEvents:UIControlEventTouchUpInside];

   return button;

    

}


- (void)animationDidStart:(CAAnimation *)anim;

{

    NSLog(@"%s\n",__FUNCTION__);

}


- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag;

{

    NSLog(@"%s\n",__FUNCTION__);

}


- (void)didReceiveMemoryWarning {

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}




0 0