如何使用返回UIBezierPath的函数来启动动画?

时间:2017-06-15 00:29:26

标签: ios swift core-animation uibezierpath

我之前使用过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")

现在我正在尝试使用一个返回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但收到警告

  

调用'circlePathWithCenter(center:radius :)'的结果未使用

以下是在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
      }

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

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

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

如果这是所需的工作流程,并且您不希望将值存储在任何位置,则只需将返回值分配给_即可。这告诉编译器你知道有一个返回值,但你对它不感兴趣。其语法就像

一样简单
_ = newLayer.circlePathWithCenter(center: point, radius: 100)

但是,如果要使用返回值,则必须将其存储在变量中并相应地使用它。