swift - 在 CAShapeLayer 中对不同角进行动画处理

标签 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 是右下角和右上角,现在我想用动画将其替换为左下角和右下角。如何做到这一点?

最佳答案

通过以下方式:

    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)

关于swift - 在 CAShapeLayer 中对不同角进行动画处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47685465/

相关文章:

Swift - 我想从 View Controller 中的 SceneDelegate.swift 访问信息

ios - 我应该做什么来设置 UICollectionView 的约束

ios - 是否可以将状态栏颜色更改为黑色?

ios - 如何迁移未存储的 Realm 对象?

ios - 在 Swift UI 中,当我使用 NavigationLink 时,屏幕上似乎没有显示任何内容

ios - ld : -bundle and -bitcode_bundle cannot be used together

ios - 更改标签栏导航 Controller 的根 Controller

ios - 如何绘制一个矩形并根据电池电量填充颜色

ios - 如何在蒙版 UIView 周围绘制阴影?

ios - 带角度的 CAGradientLayer