gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:23:04 24 4
gpt4 key购买 nike

我以前使用过 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 但收到警告

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 返回 UIBezierPath 但负责此函数调用的行未在任何地方使用返回值

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

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

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

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

关于ios - 如何使用返回 UIBezierPath 的函数来启动动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44556695/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com