CABasicAnimation - transform scale keep in center

前端 未结 4 1288
南笙
南笙 2021-02-07 02:27

Trying to animatie an ellipse masked on a UIView to be scale transformed remaining in the center position.

I have found CALayer - CABasicAnimation not scaling around cen

4条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-07 02:38

    I am not sure why, but CAShapeLayer does not set the bounds property to the layer. In my case, that's was the problem. Try setting the bounds. That should work.

    I did something like this for building the initial circle

    self.circle = [CAShapeLayer layer];
    CGRect roundedRect = CGRectMake(0, 0, width, width);
    self.circle.bounds = roundedRect;
    UIBezierPath *start = [UIBezierPath bezierPathWithRoundedRect:roundedRect cornerRadius:width/2];
    [self.circle setPath:start.CGPath];
    self.circle.position = CGPointMake(whatever suits you);
    self.circleView.layer.mask = self.circle;
    [self.circleView.layer.mask setValue: @(1) forKeyPath: @"transform.scale"];
    

    And something like this for scaling it

    CABasicAnimation *scale = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    scale.fromValue = [self.circleView.layer.mask valueForKeyPath:@"transform.scale"];
    scale.toValue = @(100);
    scale.duration = 1.0;
    scale.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [self.circleView.layer.mask setValue:scale.toValue forKeyPath:scale.keyPath];
    [self.circleView.layer.mask addAnimation:scale forKey:scale.keyPath];
    

    PD: AnchorPoint is by default (.5,.5) according to apple documentations...but we all know that does not mean anything right?

    Hope it helps!!

提交回复
热议问题