我有以下代码,可以在视图中添加“飞点” 当我返回'pathAnimation'时,动画按预期工作。但是,当我将其添加到CAAnimationGroup时,动画不会运行。我在这里做错了什么?
- (CAAnimation*)animationForPath:(CGPathRef)thePath
{
// THIS CODE WORKS ALONE - BUT NOT AS PART OF A GROUP
CAKeyframeAnimation *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];
}
}
}
答案 0 :(得分:1)
在另一个论坛上回答:
CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animation];
应该是
CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"opacity"];