iOS animate/morph shape from circle to square

后端 未结 4 1024
不思量自难忘°
不思量自难忘° 2020-12-03 03:38

I try to change a circle into a square and vice versa and I am almost there. However it doesn\'t animates as expected. I would like all corners of a square to be animated/mo

4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-03 04:26

    Based on @Danchoys answer, here it is for Objective-C.

    info is a button of 60px and infoVC is the next ViewController being pushed:

        UIBezierPath * maskPath = [UIBezierPath new];
        [maskPath addArcWithCenter:info.center radius:15.0f startAngle:DEGREES_TO_RADIANS(180) endAngle:DEGREES_TO_RADIANS(270) clockwise:true];
        [maskPath addArcWithCenter:info.center radius:15.0f startAngle:DEGREES_TO_RADIANS(270) endAngle:DEGREES_TO_RADIANS(0) clockwise:true];
        [maskPath addArcWithCenter:info.center radius:15.0f startAngle:DEGREES_TO_RADIANS(0) endAngle:DEGREES_TO_RADIANS(90) clockwise:true];
        [maskPath addArcWithCenter:info.center radius:15.0f startAngle:DEGREES_TO_RADIANS(90) endAngle:DEGREES_TO_RADIANS(180) clockwise:true];
        [maskPath closePath];
    
        UIBezierPath * endPath = [UIBezierPath new];
        [endPath moveToPoint:CGPointMake(0,0)];
        [endPath addLineToPoint:endPath.currentPoint];
        [endPath addLineToPoint:CGPointMake(w, 0)];
        [endPath addLineToPoint:endPath.currentPoint];
        [endPath addLineToPoint:CGPointMake(w, h)];
        [endPath addLineToPoint:endPath.currentPoint];
        [endPath addLineToPoint:CGPointMake(0, h)];
        [endPath addLineToPoint:endPath.currentPoint];
        [endPath closePath];
    
        CAShapeLayer *maskLayer = [CAShapeLayer layer];
        maskLayer.frame = info.bounds;
        maskLayer.path = maskPath.CGPath;
        _infoVC.view.layer.mask = maskLayer;
    
        CABasicAnimation * animation = [CABasicAnimation animationWithKeyPath:@"path"];
        animation.fromValue = (id)maskPath.CGPath;
        animation.toValue = (id)endPath.CGPath;
        animation.duration = 0.3f;
        [maskLayer addAnimation:animation forKey:nil];
        [maskLayer setPath:endPath.CGPath];
    

提交回复
热议问题