Animating CAShapeLayer size change

前端 未结 4 1571
面向向阳花
面向向阳花 2020-12-13 10:53

I am drawing a circle, with an initial radius of 200

self.circle = [CAShapeLayer layer];
self.circle.fillColor = nil;
self.circle.strokeColor = [UIColor blac         


        
4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-13 11:18

    Here is a direct swift conversion of Jonathan's code should anyone need it.

    func scaleShape(shape : CAShapeLayer){
    
        let newRadius : CGFloat = 50;
    
        let newPath: UIBezierPath = UIBezierPath(arcCenter: CGPointMake(newRadius, newRadius), radius: newRadius, startAngle: (CGFloat(-M_PI) / 2.0), endAngle: (3 * CGFloat(M_PI) / 2), clockwise: true);
    
    
        let newBounds : CGRect = CGRect(x: 0, y: 0, width: 2 * newRadius, height: 2 * newRadius);
        let pathAnim : CABasicAnimation = CABasicAnimation(keyPath: "path");
    
        pathAnim.toValue = newPath.CGPath;
    
        let boundsAnim : CABasicAnimation = CABasicAnimation(keyPath: "bounds");
        boundsAnim.toValue = NSValue(CGRect: newBounds);
    
        let anims: CAAnimationGroup = CAAnimationGroup();
        anims.animations = [pathAnim, boundsAnim];
        anims.removedOnCompletion = false;
        anims.duration = 2.0;
        anims.fillMode = kCAFillModeForwards;
    
        shape.addAnimation(anims, forKey: nil);
    }
    

提交回复
热议问题