Animating a non-standard layer property
Animating a non-standard layer property
- Subject: Animating a non-standard layer property
- From: Kenneth Baxter <email@hidden>
- Date: Mon, 20 Sep 2010 22:42:22 +0000 (GMT)
Hi, I have a layer where I want to animate a point, testPoint. For the moment, I want to animate the y value of the point. I have testPoint as a property of the layer. I want to get it to redisplay (and preferably also call the setTestPoint) on every frame of the animation, so I implement:
+ (BOOL)needsDisplayForKey:(NSString *)key {
if ([key isEqualToString:@"testPoint"]) {
return YES;
}
return [super needsDisplayForKey:key];
}
To see the changes as they are made, in addition to the normal synthesize of the testPoint, I have implemented the setter as follows:
- (void)setTestPoint:(CGPoint)p {
NSLog(@"Setting test point to %f, %f", p.x, p.y);
[self willChangeValueForKey:@"testPoint"];
testPoint = p;
[self didChangeValueForKey:@"testPoint"];
}
When I create the layer, I initialize the testPoint as follows:
layer.testPoint = CGPointMake(5.0f, 10.0f);
I try to animate the move of the point using:
CABasicAnimation *movePointAnimation = [CABasicAnimation animation];
movePointAnimation.keyPath = @"testPoint.y";
movePointAnimation.toValue = [NSNumber numberWithFloat:y];
movePointAnimation.duration = 5.0f;
movePointAnimation.removedOnCompletion = YES;
movePointAnimation.fillMode = kCAFillModeBoth;
movePointAnimation.repeatCount = 0;
movePointAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
[layer addAnimation:movePointAnimation forKey:@"testPoint.y"];
What I expected to happen is that I would get a bunch of calls to setTestPoint:, passing in the original x value of 5.0 and animating from the previous y point (initially 10.0) to the new point, and that my drawLayer:inContext: would have been called for each iteration.
Here's a typical run (each call to the animation separated by a line gap):
2010-09-21 08:16:36.717 CADisplay[20048:a0f] Starting animation to 4383.000000
2010-09-21 08:16:48.422 CADisplay[20048:a0f] Starting animation to 886.000000
2010-09-21 08:16:48.424 CADisplay[20048:a0f] Setting test point to 0.000000, 0.041937
2010-09-21 08:16:48.424 CADisplay[20048:a0f] Setting test point to 0.000000, 0.042831
2010-09-21 08:16:48.425 CADisplay[20048:a0f] Setting test point to 0.000000, 0.044800
2010-09-21 08:16:48.445 CADisplay[20048:a0f] Setting test point to 0.000000, 0.140321
2010-09-21 08:16:48.456 CADisplay[20048:a0f] Setting test point to 0.000000, 0.215275
2010-09-21 08:17:01.422 CADisplay[20048:a0f] Setting test point to 0.000000, 886.000000
2010-09-21 08:17:01.583 CADisplay[20048:a0f] Starting animation to 2777.000000
2010-09-21 08:17:15.142 CADisplay[20048:a0f] Starting animation to 1915.000000
2010-09-21 08:17:15.143 CADisplay[20048:a0f] Setting test point to 0.000000, 0.090642
2010-09-21 08:17:15.144 CADisplay[20048:a0f] Setting test point to 0.000000, 0.092054
2010-09-21 08:17:15.144 CADisplay[20048:a0f] Setting test point to 0.000000, 0.095602
2010-09-21 08:17:15.160 CADisplay[20048:a0f] Setting test point to 0.000000, 0.247579
2010-09-21 08:17:15.175 CADisplay[20048:a0f] Setting test point to 0.000000, 0.464084
And when I clicked on XCode, it added:
2010-09-21 08:17:26.118 CADisplay[20048:a0f] Setting test point to 0.000000, 1915.000000
...and my drawLayer:inContext: was not called at all during the animation.
What am I missing here? Why is my layer not being drawn on every frame? Why is the setTestPoint getting called with a zero X value? Why am I just getting these strange values in setTestPoint, and why for only the first tiny bit of the animation?
Also, if I wanted to animate the whole point rather than just the y value, what should I pass in? I don't see an NSValue valueWithCGPoint...
Thanks in advance
Ken_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden