ios - CAShapeLayer 出现在 View 之前

标签 ios swift calayer cashapelayer cabasicanimation

我有两个 View ,它们之间有可调整大小的线(使用 CAShapeLayer 查看)。我面临的问题是,尽管图层上有动画,但它出现的速度比 View 的变化快。我知道层的动画是完全不同的,但不明白我的错在哪里。如果我做错了什么,请告诉我。

这是我的图层动画的代码(也使用了“strokeStart”,但没有用):

override func layoutSubviews() {
    super.layoutSubviews()
    let centerX = bounds.size.width / 2
    let path = CGMutablePath()
    path.addLines(between: [CGPoint(x: centerX, y: 0), CGPoint(x: centerX, y: frame.height)])
    dashedLayer.path = path
    dashedLayer.frame = bounds

    let animation = CABasicAnimation(keyPath: "strokeEnd")
    animation.fromValue = height
    animation.toValue = bounds.size.height
    height = bounds.size.height
    animation.duration = 0.3
    dashedLayer.add(animation, forKey: nil)
}

View 的动画代码(因为修改 View ,我动画layoutIfNeeded):

UIView.animate(withDuration: 0.3, animations: {
    self.view.layoutIfNeeded()
    ...

最佳答案

我认为解决这个问题的一种简单方法是像这样为约束常量设置动画:

  @IBOutlet weak  var heightConstraint: NSLayoutConstraint!

  UIView.animate(withDuration: 3, animations: {

        self. heightConstaint.constant = 300 // This constant is the height of top big view. 

        self.view.layoutIfNeeded()}

CAShapeLayer 所在的 View 还有一个,我称之为line View。它的高度应限制为与 top big view 的高度相同。

行 View 中,您可以添加动画。这应该类似于您的实现。

   override func didMoveToSuperview() {
     super.didMoveToSuperview()
     dashedLayer.backgroundColor = UIColor.clear.cgColor
     layer.addSublayer(dashedLayer)
     height = bounds.size.height
   }

  override func layoutSubviews() {
    super.layoutSubviews()
    let centerX = bounds.size.width / 2
    let path = CGMutablePath()
    path.addLines(between: [CGPoint(x: centerX, y: 0), CGPoint(x: centerX, y: frame.height)])
    dashedLayer.path = path
    dashedLayer.lineWidth = 3.0
    dashedLayer.strokeColor = UIColor.blue.cgColor

    let animation = CABasicAnimation(keyPath: "strokeEnd")
    animation.fromValue = height / bounds.size.height
    animation.toValue = 1.0 
    animation.timingFunction = CAMediaTimingFunction.init(name: CAMediaTimingFunctionName.easeInEaseOut)
    height = bounds.size.height
    animation.duration = 3.0
    dashedLayer.add(animation, forKey: nil)
}

这里正在扩张。您可以自行调整代码以适应合约。

enter image description here

以下是倒车动画。

override func didMoveToSuperview() {
    super.didMoveToSuperview()
    dashedLayer.backgroundColor = UIColor.clear.cgColor
    layer.addSublayer(dashedLayer)
    height = bounds.size.height
      clipsToBounds = true
}


override func layoutSubviews() {
    super.layoutSubviews()
    let centerX = bounds.size.width / 2
    let path = CGMutablePath()
    path.addLines(between: [CGPoint(x: centerX, y: 0), CGPoint(x: centerX, y: max(height , bounds.size.height) )])
    dashedLayer.path = path
    dashedLayer.lineWidth = 3.0
    dashedLayer.strokeColor = UIColor.blue.cgColor

    let animation = CABasicAnimation(keyPath: "strokeEnd")

    if (height < bounds.size.height){
    animation.fromValue = height / bounds.size.height
        animation.toValue = 1.0}

    else {
        animation.fromValue = 1.0
        animation.toValue = bounds.size.height / height}


    animation.timingFunction = CAMediaTimingFunction.init(name: CAMediaTimingFunctionName.easeInEaseOut)
    height = bounds.size.height
    animation.duration = 3
    dashedLayer.add(animation, forKey: nil)
}

enter image description here

关于ios - CAShapeLayer 出现在 View 之前,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55238492/

相关文章:

ios - Swift 中的链接动画

ios - 如何初始化 Firestore 文档引用 Swift

ios - 动态高度 UITableViewCell

swift - Swift 中的主菜单

ios - 通过 AVExportSession 导出带有隐式动画的 CALayer

iOS - 如何删除以前添加的 UIView 子层

iphone - 避免自动释放 NSMutablearray ..... iphone 应用程序

ios - Swift/Objective C - performTransitionOperation 崩溃

swift - 动画 Collection View swift

swift - 如何在自定义 CALayer 子类的重写绘图函数中在 macOS 上绘制字符串?