Animation not starting when part of CAAnimationGroup
I have the following code which adds 'flying dots' to a view.
When I return 'pathAnimation', then the animation works as expected. When I add it to a CAAnimationGroup however, then the animation doesn't run. What am I doing wrong here?- (CAAnimation*)animationForPath:(CGPathRef)thePath
{
// THIS CODE WORKS ALONE - BUT NOT AS PART OF A GROUP
CAKeyframeAnimation 开发者_如何学C*pathAnimation = [CAKeyframeAnimation animation];
pathAnimation.path = thePath;
pathAnimation.duration = 3; // two seconds
pathAnimation.repeatCount = 10000; // "forever"
return pathAnimation;
// THIS CODE DOESN'T WORK
// CAAnimationGroup *animationGroup = [CAAnimationGroup animation];
// animationGroup.duration = 3.0;
// animationGroup.repeatCount = 10000;
// animationGroup.animations = [NSArray arrayWithObjects:pathAnimation, nil];
// return animationGroup;
}
- (void) animateLayer:(CALayer*)layer fromXOffset:(CGFloat)x_offset timeOffset:(CGFloat)timeOffset
{
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, x_offset, 0);
CGPathAddLineToPoint(path, NULL, x_offset, 1000);
CAAnimation* animation = [self animationForPath:path];
animation.timeOffset = timeOffset;
[layer addAnimation:animation forKey:@"position"];
CGPathRelease(path);
}
- (void) addDotsToView:(UIView*)view
{
int numDots = 10;
CGFloat spacing = view.bounds.size.width / (numDots * 1.0f);
for(int i = 0; i < 10; ++i)
{
for(CGFloat offset = 10; offset < view.bounds.size.width; offset += spacing)
{
layer_ = [CALayer layer];
[layer_ setFrame:CGRectMake(0, 0, 10, 10)];
[layer_ setBackgroundColor:[[UIColor whiteColor] CGColor]];
[view.layer addSublayer:layer_];
[self animateLayer:layer_ fromXOffset:offset timeOffset:i * 0.5f];
}
}
}
Answered in another forum:
CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animation];
should be
CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"opacity"];
精彩评论