Core Animation rendering problem
Core Animation rendering problem
- Subject: Core Animation rendering problem
- From: Randall Meadows <email@hidden>
- Date: Wed, 22 Jul 2009 23:05:49 -0600
Probably not the best subject line, but best I could come up with...
I an animating a ball across the screen, using this code:
CABasicAnimation *animation = [CABasicAnimation
animationWithKeyPath:@"position"];
animation.duration = duration;
animation.delegate = self;
animation.timingFunction = [CAMediaTimingFunction
functionWithName:kCAMediaTimingFunctionLinear];
animation.fromValue = [NSValue valueWithCGPoint:self.center];
animation.toValue = [NSValue valueWithCGPoint:intersectPt];
self.layer.position = intersectPt;
[self.layer addAnimation:animation forKey:@"position"];
No problem. Works great. Now, something happens and I want to stop
the motion of the ball, and start a new animation that increases the
diameter of the ball. I do so by doing
[self.layer removeAllAnimations];
if (speed > 0) {
self.speed = 0.0;
CGPoint here = ((CALayer *)(self.layer.presentationLayer)).position;
self.center = here;
}
to stop the ball in place (which seems to work, as the following
animation is done at the correct location) and then I start a new
animation that increases the size of the ball, like so:
[UIView beginAnimations:nil context:NULL]; {
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelegate:self];
[UIView
setAnimationDidStopSelector:@selector(explosion:finished:context:)];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
CGFloat delta = (kParticleSizeExplode - kParticleSizeNormal);
CGRect newBounds = self.bounds;
newBounds.origin.x -= delta/2.0;
newBounds.size.width += delta;
newBounds.origin.y -= delta/2.0;
newBounds.size.height += delta;
self.bounds = newBounds;
} [UIView commitAnimations];
The problem occurs while drawing the ball during this 2nd animation;
instead of redrawing the ball at increasing sizes, so it's nice and
round, instead it appears that the small ball is simply being scaled
up, and is very jaggy.
Here's my -drawRect: for the ball:
CGContextRef context = UIGraphicsGetCurrentContext();
const CGFloat *components = CGColorGetComponents(self.color);
CGContextSetRGBFillColor(context, components[0], components[1],
components[2], 1.0);
CGContextFillEllipseInRect(context, [self currentBounds]);
The -currentBounds method is a convenience method to get the current
bounds of the object, whether it is part of an animation currently or
not:
- (CGRect)currentBounds
{
CGRect bounds = CGRectZero;
CALayer *presLayer = self.layer.presentationLayer;
if (presLayer == nil) {
bounds = self.bounds;
}
else {
bounds = ((CALayer *)presLayer).bounds;
}
return bounds;
}
Any idea why the drawing is screwing up for the 2nd animation? Are
the 2 different methods of animating messing up each other somehow
(even though I've removed the 1st animation)?
Tips/suggestions/comments appreciated.
randy
_______________________________________________
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