2013-06-26 25 views
1

上我需要重新绘制绘制圆多于一个的动画,我用这个代码进行动画的描绘删除或重新加进CABasicAnimation uivew层

CAShapeLayer *circle = [CAShapeLayer layer]; 
     // Make a circular shape 
     circle.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 2.0*radius, 2.0*radius) 
               cornerRadius:radius].CGPath; 
     // Center the shape in self.view 
     circle.position = CGPointMake(CGRectGetMidX(_DrawingView.frame)-radius, 
             CGRectGetMidY(_DrawingView.frame)-radius-0.66*radius); 

     // Configure the apperence of the circle 
     circle.fillColor = [UIColor clearColor].CGColor; 
     circle.strokeColor = [UIColor redColor].CGColor; 
     circle.lineWidth = 15; 

     // Add to parent layer 
     [_DrawingView.layer addSublayer:circle]; 

     // Configure animation 
     CABasicAnimation *drawAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"]; 
     drawAnimation.duration   = 10.0; // "animate over 10 seconds or so.." 
     drawAnimation.repeatCount   = 1.0; // Animate only once.. 
     drawAnimation.removedOnCompletion = NO; // Remain stroked after the animation.. 

     // Animate from no part of the stroke being drawn to the entire stroke being drawn 
     drawAnimation.fromValue = [NSNumber numberWithFloat:0.0f]; 
     drawAnimation.toValue = [NSNumber numberWithFloat:1.0f]; 

     // Experiment with timing to get the appearence to look the way you want 
     drawAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]; 

     // Add the animation to the circle 
     [circle addAnimation:drawAnimation forKey:@"drawCircleAnimation"]; 

我需要删除动画再次重绘它,我试过这种

[_DrawingView.layer removeAnimationForKey:@"drawCircleAnimation"]; 

,但没有奏效,所以我怎么能删除该层的动画?

+0

,设置repeatcount到该号码,而不是1 –

+0

我想再说一遍当完成 –

+0

后,按钮不会自动按下,然后将此代码写入单击按钮时调用的方法中。 –

回答

1

想通了!

我加入[email protected]"circle"; 那么当按下按钮我检查图层名称,如果你想重复动画删除

for (CALayer *layer in _DrawingView.layer.sublayers) { 
      if ([layer.name isEqualToString:@"circle"]) { 
       [layer removeFromSuperlayer]; 
       break; 
      } 
     }