We can add animation to view using CABasicAnimation. Here we are adding bounce effect to UIImageView.
The following code will show the bounce effect in the UIImageView at y-axis:-
CGPoint origin = self.imgView.center; // self.imgView is the outlet on UIImageView
CGPoint target = CGPointMake(self.imgView.center.x, self.imgView.center.y+100);
CABasicAnimation *bounce = [CABasicAnimation animationWithKeyPath:@"position.y"];
bounce.duration = 0.5;
bounce.fromValue = [NSNumber numberWithInt:origin.y];
bounce.toValue = [NSNumber numberWithInt:target.y];
bounce.repeatCount = 3;
bounce.autoreverses = YES;
[self.imgView.layer addAnimation:bounce forKey:@"position"];
In the above code we are setting the bounce position along the y-axis. We also set the duration for the bounce and the count for the image to bounce i.e how many times the UIImage will bounce .
We can also set the bounce effect of UIImageView along x-axis as :-
CGPoint origin = self.imgView.center;
CGPoint target = CGPointMake(self.imgView.center.x-100, self.imgView.center.y);
CABasicAnimation *bounce = [CABasicAnimation animationWithKeyPath:@"position.x"];
bounce.duration = 0.5;
bounce.fromValue = [NSNumber numberWithInt:origin.x];
bounce.toValue = [NSNumber numberWithInt:target.x];
bounce.repeatCount = 3;
bounce.autoreverses = YES;
[self.imgView.layer addAnimation:bounce forKey:@"position"];
Note :- We also have to include QuartzCore framework in our project !!
0 Comment(s)