2017-06-15 84 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) 

但是,如果你想使用的返回值,那麼你就必須將其存儲在一個變量,並相應地使用它。