2011-04-30 41 views
1

我有以下代碼將「飛點」添加到視圖中。
當我返回'pathAnimation'時,動畫按預期工作。但是,當我將它添加到CAAnimationGroup時,動畫不會運行。我在這裏做錯了什麼?當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]; 
     } 
    } 
} 

回答

1

在另一個論壇回答:

CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animation]; 

應該

CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"opacity"];