Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Adding bounce effect to UIImageView .

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.05k
    Comment on it

    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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: