iOS animate/morph shape from circle to square

后端 未结 4 1027
不思量自难忘°
不思量自难忘° 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:17

    I know this is an old question but this might help someone.

    I think its better to animate corner radius to get this effect.

        let v1 = UIView(frame: CGRect(x: 100, y: 100, width: 50, height: 50))
        v1.backgroundColor = UIColor.green
        view.addSubview(v1)
    

    animation:

        let animation = CABasicAnimation(keyPath: "cornerRadius")
        animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
        animation.fillMode = kCAFillModeForwards
        animation.isRemovedOnCompletion = false
        animation.fromValue = v1.layer.cornerRadius
        animation.toValue = v1.bounds.width/2
        animation.duration = 3
        v1.layer.add(animation, forKey: "cornerRadius")
    

提交回复
热议问题