2

I am just trying to translate the layer back to its original position, after already have translated it once. I can always store the layer position I translated to in a property, but it seems like there is a better way. I am doing something like this to translate my layer:

CABasicAnimation *slide = [CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
slide.byValue = [NSNumber numberWithFloat:translationValue];
slide.duration = duration;
slide.removedOnCompletion = NO;
slide.fillMode = kCAFillModeForwards;
slide.autoreverses = NO;

[layer addAnimation:slide forKey:KEYPATH_POSITION];

I was trying to do something like the following, but this has no animation, it just appears back to its original position.

CABasicAnimation *slide = [CABasicAnimation animationWithKeyPath:@"position"];
slide.toValue = [layer valueForKey:@"position"];
slide.duration = duration;
slide.removedOnCompletion = NO;
slide.fillMode = kCAFillModeForwards;
slide.autoreverses = NO;

[layer addAnimation:slide forKey:KEYPATH_POSITION];
SirRupertIII
  • 12,324
  • 20
  • 72
  • 121
  • 1
    `removedOnCompletion = NO` is your problem. Set the model value instead and remove the animation when it completes – David Rönnqvist Nov 07 '13 at 16:49
  • 1
    [My answer here](http://stackoverflow.com/a/17435092/608157) could be helpful (repeated animations with removedOnCompletion=NO). – David Rönnqvist Nov 07 '13 at 16:56
  • Sweet, I understand this a lot more now. I can totally see why your way is better. Would you then just keep the original starting point in a property? So that I can just set the transform to be that point, when I want the layer to move back? – SirRupertIII Nov 07 '13 at 17:40
  • If you are using a transform like your first example then you can simply set it back the identity transform to move back – David Rönnqvist Nov 07 '13 at 17:57
  • Sorry, I don't really understand. I tried doing something like the following but it didn't work: CGPoint currentPosition = [[layer valueForKeyPath:KEYPATH_POSITION] CGPointValue]; slide.toValue = [NSValue valueWithCGPoint:currentPosition]; – SirRupertIII Nov 07 '13 at 18:28
  • Ah, I figured it out. If you want to write an answer, I'll mark your answer, if not, I'll just post code I'm using as an answer. Thank you! – SirRupertIII Nov 07 '13 at 19:09
  • It's ok. You can post the answer yourself :) – David Rönnqvist Nov 07 '13 at 21:42

0 Answers0