Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to increase and decrease the image alpha value with animation in ios sdk?

Tags:

ios

iphone

In this code i am using animation.But at a time i need to change the image alpha value also.

-(void)animation
{
    CGPoint point = CGPointMake(imgView.frame.origin.x, imgView.frame.origin.y);
    imgView.layer.position = point;
    CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"position.x"];
    anim.fromValue  = [NSValue valueWithCGPoint:point];
    anim.toValue    = [NSValue valueWithCGPoint:CGPointMake(point.x + 50, point.y)];
    anim.duration   = 10.0f;
    anim.repeatCount =1;
    anim.removedOnCompletion = YES;

    anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];

    [imgView.layer addAnimation:anim forKey:@"position.x"];
    imgView.layer.position = point;
}
like image 218
raja Avatar asked Sep 06 '13 12:09

raja


1 Answers

You can use the opacity key of CABasicAnimation for doing this:

CABasicAnimation *alphaAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
alphaAnimation.fromValue = [NSNumber numberWithFloat:1.0];
alphaAnimation.toValue = [NSNumber numberWithFloat:0.0];
[imgView.layer addAnimation:alphaAnimation forKey:@"opacity"];

Edit:

For animating through specified values you can use CAKeyframeAnimation:

CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"opacity"];
    animation.duration = 10.0f;
    animation.repeatCount = 1;
    animation.values  =  [NSArray arrayWithObjects:
                          [NSNumber numberWithFloat:1.0,
                          [NSNumber numberWithFloat:0.5],
                          [NSNumber numberWithFloat:1.0],nil];
    animation.keyTimes = [NSArray arrayWithObjects:
                          [NSNumber numberWithFloat:0.0],
                          [NSNumber numberWithFloat:5.0],
                          [NSNumber numberWithFloat:10.0], nil];
    [imgView.layer addAnimation:animation forKey:@"opacity"];
like image 191
Midhun MP Avatar answered Oct 09 '22 18:10

Midhun MP