Question

Is this a good way to use position to move a CALayer permanently? Do I really need a to and from value animation?

serveBlock2 = [CALayer layer];
serveBlock2.zPosition = 1;
[serveBlock2 setFrame:CGRectMake(screenBounds.size.height/2, 0, screenBounds.size.height/2, screenBounds.size.width)];
[serveBlock2 setOpacity:0.0f];
[serveBlock2 setBackgroundColor:[UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.8f].CGColor];
[self.view.layer addSublayer:serveBlock2];

CABasicAnimation *updateCurrentServe2 = [CABasicAnimation animationWithKeyPath:@"position.y"];
serveBlock2.position = CGPointMake((screenBounds.size.height/4)*3, -screenBounds.size.width/2);
[updateCurrentServe2 setTimingFunction:[CAMediaTimingFunction functionWithControlPoints:0.8 :-0.8 :1.0 :1.0]];
[updateCurrentServe2 setDuration:1.5];
[serveBlock2 addAnimation:updateCurrentServe2 forKey:@"serveBlock2 updateCurrentServe2"];
Was it helpful?

Solution 2

You have two questions there and I will answer them in the opposite order:

Do I really need a to and from value animation?

No, you don't need that.

CABasicAnimation has three properties that go together define how the interpolation is done, these are fromValue, byValue and toValue. They can be combined in many different ways which is listed under the "Setting Interpolation Values" section in the CABasicAnimation documentation. The very last listed combination is:

  • All properties are nil. Interpolates between the previous value of keyPath in the target layer’s presentation layer and the current value of keyPath in the target layer’s presentation layer.

So what you are doing and the results you are seeing is documented behavior and works.

Is this a good way to use position to move a CALayer permanently?

This is a very opinionated question and I can only give you my personal opinion and try and explain my reasoning.

I personally don't like it because it is less explicit and you would need to have read this special case in the documentation to know how it works. As you saw in the comment on your question I wasn't even sure how this worked when I first saw it and I consider myself very familiar with Core Animation and have read the documentation many times.

It all should come down to what you and your team thinks is most readable and clear.

Personally I prefer to explicitly set the new model value and then only specify the fromValue, i.e. this case:

  • fromValue is non-nil. Interpolates between `fromValue and the current presentation value of the property.

OTHER TIPS

It isn't clear what you are asking.

Core Animation does not actually move the position of the layers it animates. It creates the appearance that the layer is changing (by making changes to the layer's presentation layer) but does not actually change the underlying property. Usually what you do is to start the animation running, then add extra statements to set the property/properties you are changing to their end values. Then when the animation completes the object is in it's final location.

When you create a CAAnimation, you can supply a from and to value or just a to value. If you only provide a to value, it will animate from it's previous position to the new position.

If you just want to move your layer, set it's position property without an animation.

If the layer is the backing layer of a view, it will simply jump to it's new location.

If the layer is some other sublayer, you will get an implicit animation from the old value to the new value.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top