在CAShapeLayer中为不同的角设置动画

时间:2017-12-06 23:58:42

标签: swift xcode cashapelayer

我使用以下代码:

let MainLayer : CAShapeLayer = {
    let mainlayer = CAShapeLayer()
    mainlayer.fillColor = UIColor.white.cgColor
    mainlayer.shadowColor = UIColor.darkGray.cgColor
    mainlayer.shadowOffset = CGSize(width: 3.0, height: 3.0)
    mainlayer.shadowOpacity = 0.5
    mainlayer.shadowRadius = 7
    return mainlayer
}()
var LayerRect:CGRect!
var bezierPath:UIBezierPath!
let CellHeightMargin:CGFloat = 6.0
let CellWidthMargin:CGFloat = 15.0


override func viewDidLoad() {
    super.viewDidLoad()
    LayerRect = CGRect(x: self.bounds.origin.x, y: self.bounds.origin.y, width: UIScreen.main.bounds.width - CellWidthMargin * 2, height: self.bounds.height - CellHeightMargin * 2)
    bezierPath = UIBezierPath(roundedRect: LayerRect, byRoundingCorners: [.bottomRight,.topRight], cornerRadii: CGSize(width: 5, height: 5))
    MainLayer.path = bezierPath.cgPath
    self.mainView.layer.insertSublayer(MainLayer, at: 0)

}

正如你所看到的,byRoundingCorners是右下角和右上角,现在我想用左下角和右下角用动画替换它。怎么做?

1 个答案:

答案 0 :(得分:0)

通过以下方式:

    let ToStatusLayerPath = UIBezierPath(roundedRect: CellView.statusView.bounds, byRoundingCorners: [.bottomRight,.bottomLeft], cornerRadii: CGSize(width: 5, height: 5))
    let animation = CABasicAnimation(keyPath: "path")
    animation.toValue   = ToStatusLayerPath.cgPath
    animation.duration  = 1.0;
    animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)
    animation.fillMode = kCAFillModeForwards
    animation.isRemovedOnCompletion = false
    statusLayer.add(animation, forKey: nil)