Wednesday 16 July 2014

CABasicAnimation Examples

UIView *AnimView1=[[UIView alloc] initWithFrame:CGRectMake(30, 50, 50, 50)];
AnimView1.backgroundColor=[UIColor greenColor];
[self.view addSubview:AnimView1];

UIView *AnimView2=[[UIView alloc] initWithFrame:CGRectMake(90, 75, 50, 50)];
AnimView2.backgroundColor=[UIColor orangeColor];
[self.view addSubview:AnimView2];

UIView *AnimView3=[[UIView alloc] initWithFrame:CGRectMake(170, 90, 50, 50)];
AnimView3.backgroundColor=[UIColor yellowColor];
[self.view addSubview:AnimView3];

UIView *AnimView4=[[UIView alloc] initWithFrame:CGRectMake(240, 150, 50, 50)];
AnimView4.backgroundColor=[UIColor blueColor];
[self.view addSubview:AnimView4];

//shrink
CABasicAnimation* shrink = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
shrink.toValue = [NSNumber numberWithDouble:0.5];
shrink.duration = 0.5;
shrink.delegate = self;
shrink.repeatCount=INFINITY;
shrink.autoreverses=YES;
[[AnimView1 layer] addAnimation:shrink forKey:@"shrinkAnim"];

//moving
CABasicAnimation *Animation1;
Animation1=[CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
Animation1.duration=0.2;
Animation1.repeatCount=INFINITY;
Animation1.autoreverses=YES;
Animation1.fromValue=[NSNumber numberWithFloat:0];
Animation1.toValue=[NSNumber numberWithFloat:-10];
[[AnimView2 layer] addAnimation:Animation1 forKey:@"shakeAnim"];

//rotating
CABasicAnimation *Animation2;
Animation2=[CABasicAnimation animationWithKeyPath:@"transform.rotation"];
Animation2.duration=0.2;
Animation2.repeatCount=INFINITY;
Animation2.autoreverses=YES;
Animation2.fromValue=[NSNumber numberWithFloat:0];
Animation2.toValue=[NSNumber numberWithFloat:M_PI/4];
[[AnimView3 layer] addAnimation:Animation2 forKey:@"rotateAnim"];

//fade
CABasicAnimation *animation3 = [CABasicAnimation animationWithKeyPath:@"opacity"];
animation3.duration=0.5;
animation3.repeatCount=INFINITY;
animation3.autoreverses=YES;
animation3.fromValue=[NSNumber numberWithFloat:1];
animation3.toValue=[NSNumber numberWithFloat:0];
[[AnimView4 layer] addAnimation:animation3 forKey:@"fadeAnim"];  
Reference

No comments:

Post a Comment