Animating CAShapeLayer size change

前端 未结 4 1573
面向向阳花
面向向阳花 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:17

    You can use a CAAnimationGroup to animate the bounds and path of your CAShapeLayer:

    - (void)animateToRadius:(CGFloat)newRadius {
       CAShapeLayer *shapeLayer = ...;
    
       UIBezierPath *newBezierPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(newRadius, newRadius)];
       NSValue *newBounds = [NSValue valueWithCGRect:CGRectMake(0,0,2*newRadius,2*newRadius);
    
       CAAnimationGroup *group = [CAAnimationGroup animation];
    
       NSMutableArray *animations  = [@[] mutableCopy];
       NSDictionary *animationData = @{@"path":newBezierPath.CGPath, @"bounds":newBounds};
       for(NSString *keyPath in animationData) {
          CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:keyPath];
          animation.toValue   = animationData[keyPath];
          [animations addObject:animation];
       }
    
       group.animations = [animations copy];
       group.duration = 2;
       group.removedOnCompletion = NO;
       group.fillMode = kCAFillModeForwards;
    
       [shapeLayer addAnimation:group forKey:nil];
    }
    

提交回复
热议问题