如何使用 returns 一个 UIBezierPath 的函数来启动动画?

How to use a function that returns an UIBezierPath to start an animation?

我以前使用过 coreAnimation,但不是作为单独的函数,而是像这样:

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

    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")

现在我正在尝试使用 returns 一个 UIBezierPath 但不确定如何正确使用它的函数

 func circlePathWithCenter(center: CGPoint, radius: CGFloat) -> UIBezierPath {
    let circlePath = UIBezierPath()
    circlePath.addArc(withCenter: center, radius: radius, startAngle: -CGFloat.pi, endAngle: -CGFloat.pi/2, clockwise: true)
    circlePath.addArc(withCenter: center, radius: radius, startAngle: -CGFloat.pi/2, endAngle: 0, clockwise: true)
    circlePath.addArc(withCenter: center, radius: radius, startAngle: 0, endAngle: CGFloat.pi/2, clockwise: true)
    circlePath.addArc(withCenter: center, radius: radius, startAngle: CGFloat.pi/2, endAngle: CGFloat.pi, clockwise: true)
    circlePath.close()
    return circlePath
  }

我尝试创建自定义 CALayer,但收到警告

Result of call to 'circlePathWithCenter(center:radius:)' is unused

这里是调用viewController

中函数的代码
 @IBAction func buttonPressed(_ sender: Any) {

    let point = CGPoint(x:self.view.frame.origin.x, y: 100)

//morphLayer is the name of the custom CALayer
        let newLayer = morphLayer(frame: CGRect(x: 10, y: 10, width: 100, height: 100))
        newLayer.circlePathWithCenter(center: point, radius: 100) // warning here
      }

任何帮助将不胜感激!

您收到警告是因为函数 circlePathWithCenter return 是 UIBezierPath 但负责此函数调用的行未在任何地方使用 return 值

newLayer.circlePathWithCenter(center: point, radius: 100)

如果这是所需的工作流程并且您不想将值存储在任何地方,您可以将 return 值分配给 _。这告诉编译器你知道有一个 return 值,但你对它不感兴趣。语法很简单

_ = newLayer.circlePathWithCenter(center: point, radius: 100)

但是,如果您想使用 return 值,则必须将它存储在一个变量中并相应地使用它。