【问题标题】:Remove or reanimate CABasicAnimation on uivew layer在 uivew 层上删除或重新设置 CABasicAnimation
【发布时间】:2013-06-26 15:25:28
【问题描述】:

我需要重绘不止一个画圈的动画,我用这段代码来动画绘图

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"];

但它不起作用,那么如何从图层中删除动画?

【问题讨论】:

  • 如果要重复动画,请将重复计数设置为该数字而不是 1。
  • 我想在按钮完成后不自动按下时重复它
  • 然后在点击按钮时调用的方法中编写这段代码。
  • 我这样做了,但是当圆圈没有被删除时,它会覆盖旧圆圈

标签: iphone ios objective-c uiview cabasicanimation


【解决方案1】:

想通了!

我添加了circle.name=@"circle"; 然后当按下按钮时,我检查图层名称并将其删除

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-19
    • 1970-01-01
    • 2011-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多