Tanner
Tanner

Reputation: 183

UIImage view resets after animation is called

Im new to core animation. Every time I call the animation my image moves from its location to the top left of the screen, moves, then goes back to the original location. How do I make the image move 50 units left, stop, then repeat if the button is pressed again. Code:

-(IBAction)preform:(id)sender{

CGPoint point = CGPointMake(50, 50);

CABasicAnimation *anim = [CABasicAnimation animationWithKeyPath:@"position"];
anim.fromValue  = [NSValue valueWithCGPoint:point];
anim.toValue    = [NSValue valueWithCGPoint:CGPointMake(point.x + 50, point.y)];
anim.duration   = 1.5f;
anim.repeatCount =1;
anim.removedOnCompletion = YES;
anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
[imView.layer  addAnimation:anim forKey:@"position"];

}

Upvotes: 0

Views: 484

Answers (1)

Dmitry Shevchenko
Dmitry Shevchenko

Reputation: 32404

For your case, you can skip heavy CA machinery and use block animations

// Your image is at starting position
[UIView animateWithDuration:1.5 animations:^{
        CGRect newFrame = CGRectOffset(imView.frame, 50, 0);
        imView.frame = newFrame;
    }];

But, if you want to know how to handle this "snap back" correctly, check this url.

Upvotes: 1

Related Questions