Reputation: 22569
I have successfully animated my layers' position and transform (rotation).
Now, I needed to animate the anchorPoint, but failed at all attempts! Even when I do this:
2012... App[6437:403] layer: 1, KeyPath: anchorPoint, fromValue: NSPoint: {1.05, 0.5}, toValue: NSPoint: {1.05, 0.5}, Duration: 2.5
This is an NSLog stuck before animating the layer.. The result I get is the layer moving to the left in the X-Axis waaaay too far, then coming back .. and repeating (since autoreverse is ON).
Note about the layers hierarchy: SuperLayer has three sublayers that I am applying the animation on.
Why? How should the anchor point be animated?
My Code:
NSArray* subKeyPath = [keyPaths objectAtIndex:tag];
NSArray* subToValue = [toValues objectAtIndex:tag];
NSArray* subDuration = [durations objectAtIndex:tag];
NSEnumerator* keyPathsEnum = [subKeyPath objectEnumerator];
NSEnumerator* toValuesEnum = [subToValue objectEnumerator];
NSEnumerator* durationsEnum = [subDuration objectEnumerator];
NSString* keyPath;
NSNumber* toValue;
NSNumber* duration;
while ((keyPath = [keyPathsEnum nextObject]) && (toValue = [toValuesEnum nextObject]) && (duration = [durationsEnum nextObject])) {
CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:keyPath];
id offsetToValue;
if ([keyPath hasPrefix:@"position"]) {
//I removed the position animation, since animating the anchorPoint should suffice
} else if ([keyPath hasPrefix:@"anchorPoint"]) {
//This was when I used anchorPoint.x float value = [[subLayer valueForKeyPath:keyPath] floatValue] - ([toValue floatValue]/[[subLayer contents] size].width);
offsetToValue = [NSNumber valueWithPoint:CGPointMake(1.05, 0.5)];
keyPath = [keyPath substringToIndex:[keyPath length]-2];
} else {
offsetToValue = toValue;
}
...
animation.toValue = offsetToValue;
animation.duration = [duration floatValue];
animation.fillMode = kCAFillModeForwards;
animation.repeatCount = INFINITY;
animation.autoreverses = YES;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
[subLayer addAnimation:animation forKey:keyPath];
//update the model
}
I tried animating the "anchorPoint.x" keyPath, same result :(
Last note, I am writing a cocoa app, but animating the X value should be exactly the same on both cocoa and cocoa touch.
Upvotes: 1
Views: 1224
Reputation: 22569
you are not gonna believe this (As if I am sitting with you from the beginning)..
Animating the position and the rotation was working properly without setting the fromValue
. However, animating the anchor point required that I set the anchor point fromValue
to the initial value!
animation.fromValue = [subLayer valueForKeyPath:keyPath];
Upvotes: 2