2017-06-15 50 views
0

我用coreAnimation但不能作为单独的功能之前,而是这样的事情:如何使用返回UIBezierPath来启动动画的函数?

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(中心:半径:)”是未使用

这里是代码调用函数中的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 
     } 

任何帮助将不胜感激!

回答

0

你是因为circlePathWithCenter返回UIBezierPath但负责此函数调用行的功能没有使用的返回值的任何地方

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

如果是这样期望的工作流程接收警告,你不想要将值存储在任何位置,只需将返回值分配给_即可。这告诉编译器,你知道有一个返回值,但你不感兴趣。对于语法很简单,只要

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

但是,如果你想使用的返回值,那么你就必须将其存储在一个变量,并相应地使用它。