CAShapeLayer
는 클래스하고 따라서 참조 형이다.
let someLine = CAShapeLayer()
은 CAShapeLayer
개체에 대한 상수입니다. 이 참조를 함수 에 전달하고 함수 내에서 참조 된 개체의 속성을 수정할 수 있습니다.
func setupLine(line: CAShapeLayer, startingPath: CGPath) {
line.path = startingPath
line.strokeColor = UIColor.whiteColor().CGColor
line.fillColor = nil
line.lineWidth = 1
}
let someLine = CAShapeLayer()
setupLine(someLine, startingPath: somePath)
가능한 대안 레이어
let someLine = CAShapeLayer(lineWithPath: somePath)
으로 생성 될 수있는 편의 이니셜 그래서
extension CAShapeLayer {
convenience init(lineWithPath path: CGPath) {
self.init()
self.path = path
self.strokeColor = UIColor.whiteColor().CGColor
self.fillColor = nil
self.lineWidth = 1
}
}
은 다음 &
운영자 또는 inout
필요 없다
놀이터를위한 완벽한 예. 기본값으로
import UIKit
class ShapedView: UIView{
override var layer: CALayer {
let path = UIBezierPath(ovalInRect:CGRect(x:0, y:0, width: self.frame.width, height: self.frame.height)).CGPath
return CAShapeLayer(lineWithPath: path)
}
}
extension CAShapeLayer {
convenience init(lineWithPath path: CGPath, strokeColor:UIColor? = .whiteColor(), fillColor:UIColor? = nil, lineWidth:CGFloat = 1) {
self.init()
self.path = path
if let strokeColor = strokeColor { self.strokeColor = strokeColor.CGColor } else {self.strokeColor = nil}
if let fillColor = fillColor { self.fillColor = fillColor.CGColor } else {self.fillColor = nil}
self.lineWidth = lineWidth
}
}
let view = ShapedView(frame: CGRect(x:0, y:0, width: 100, height: 100))
결과 : 그것이 더 다양한 있도록 기본 매개 변수를 사용합니다
당신은 모든 여기 inout' – Alexander