How to make my UIBezierPath animated with CAShapeLayer?

前端 未结 1 1897
生来不讨喜
生来不讨喜 2020-12-05 07:52

I\'m trying to animate a UIBezierPath and I\'ve installed a CAShapeLayer to try to do it. Unfortunately the animation isn\'t working and I\'m not sure any of the layers are

相关标签:
1条回答
  • 2020-12-05 08:41

    It looks like you're trying to animate within drawRect (indirectly, at least). That doesn't quite make sense. You don't animate within drawRect. The drawRect is used for drawing a single frame. Some animation is done with timers or CADisplayLink that repeatedly calls setNeedsDisplay (which will cause iOS to call your drawRect) during which you might draw the single frame that shows the progress of the animation at that point. But you simply don't have drawRect initiating any animation on its own.

    But, since you're using Core Animation's CAShapeLayer and CABasicAnimation, you don't need a custom drawRect at all. Quartz's Core Animation just takes care of everything for you. For example, here is my code for animating the drawing of a UIBezierPath:

    #import <QuartzCore/QuartzCore.h>
    
    @interface View ()
    @property (nonatomic, weak) CAShapeLayer *pathLayer;
    @end
    
    @implementation View
    
    /*
    // I'm not doing anything here, so I can comment this out
    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            // Initialization code
        }
        return self;
    }
    */
    
    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    - (void)drawRect:(CGRect)rect
    {
        // Drawing code
    }
    */
    
    // It doesn't matter what my path is. I could make it anything I wanted.
    
    - (UIBezierPath *)samplePath
    {
        UIBezierPath *path = [UIBezierPath bezierPath];
    
        // build the path here
    
        return path;
    }
    
    - (void)startAnimation
    {
        if (self.pathLayer == nil)
        {
            CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    
            shapeLayer.path = [[self samplePath] CGPath];
            shapeLayer.strokeColor = [[UIColor grayColor] CGColor];
            shapeLayer.fillColor = nil;
            shapeLayer.lineWidth = 1.5f;
            shapeLayer.lineJoin = kCALineJoinBevel;
    
            [self.layer addSublayer:shapeLayer];
    
            self.pathLayer = shapeLayer;
        }
    
        CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
        pathAnimation.duration = 3.0;
        pathAnimation.fromValue = @(0.0f);
        pathAnimation.toValue = @(1.0f);
        [self.pathLayer addAnimation:pathAnimation forKey:@"strokeEnd"];
    }
    
    @end
    

    Then, when I want to start drawing the animation, I just call my startAnimation method. I probably don't even need a UIView subclass at all for something as simple as this, since I'm not actually changing any UIView behavior. There are definitely times that you subclass UIView with a custom drawRect implementation, but it's not needed here.

    You asked for some references:

    • I would probably start with a review of Apple's Core Animation Programming Guide, if you haven't seen that.
    • For me, it all fell into place when I went through Mike Nachbaur's Core Animation Tutorial Part 4, actually reproducing his demo from scratch. Clearly, you can check out parts 1 through 3, too.
    0 讨论(0)
提交回复
热议问题