zakdances
zakdances

Reputation: 23685

How do I animate origin?

I want to animate the position of a view based on the value of its top left point (the origin). I've tried both

CGPoint myPosition = CGPointMake(0, 500);
CABasicAnimation *yAnimation = [CABasicAnimation animationWithKeyPath:@"frameOrigin"];
yAnimation.toValue = [NSValue valueWithCGPoint:myPosition];

and

CGPoint myPosition = CGPointMake(0, 500);
CABasicAnimation *yAnimation = [CABasicAnimation animationWithKeyPath:@"origin"];
yAnimation.toValue = [NSValue valueWithCGPoint:myPosition];

and nothing works. How can I animate this property correctly?

Upvotes: 1

Views: 1233

Answers (2)

StuStirling
StuStirling

Reputation: 16191

If you just want to animate the position of the view have a look at UIViewAnimation and using a block with it.

[UIView animateWithDuration:.7
        animations:^{
           //what you would like to animate
           myView.frame = CGRectMake(myView.frame.origin.x + 100,myView.frame.origin.y + 100, myView.frame.size.width, myView.frame.size.height);
        }completion:^(BOOL finished){
          //do something when the animation finishes
        }];

Upvotes: 2

wattson12
wattson12

Reputation: 11174

There is no origin or frameOrigin key path on a CALayer, if you want to animate the position you will need to animate the bounds property

Upvotes: 1

Related Questions