【问题标题】:How to animate update position of CAReplicatorLayer in swift?如何在 swift 中为 CAReplicatorLayer 的更新位置设置动画?
【发布时间】:2019-03-15 07:08:05
【问题描述】:

我需要为我的 CAReplicatorLayer 对象设置 50 像素以上的动画。

我实现了将 CAReplicatorLayer 帧更新到 50 像素以上,但我没有通过动画实现。

这是我的代码。

 CATransaction.begin();
 CATransaction.setAnimationDuration(2.0)
 self.rippleLayer.position = self.imgLogo.center //rippleLayer is my CAReplicatorLayer object and self.imgLogo.center is like 50 pixels up for refernce.
 CATransaction.setDisableActions(true)
 CATransaction.commit();

【问题讨论】:

  • 好吧,setDisableActions(true) 的意思是“不要动画”。所以你不做动画。
  • @matt,如果我删除 setDisableActions 那么我的应用程序就会崩溃

标签: ios iphone swift core-animation calayer


【解决方案1】:

我的代码证明你的代码没有问题。如果它崩溃了,肯定还有其他一些原因。

var rippleLayer: CAReplicatorLayer!
override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    rippleLayer = CAReplicatorLayer.init()
    rippleLayer.frame = CGRect.init(x: 0, y: 0, width: 100, height: 100)
    rippleLayer.backgroundColor = UIColor.red.cgColor
    self.view.layer.addSublayer(rippleLayer)
    Timer.scheduledTimer(withTimeInterval: 2.0, repeats: false) { _ in
        CATransaction.begin();
        CATransaction.setAnimationDuration(2.0)
        self.rippleLayer.position = self.view.center //rippleLayer is my CAReplicatorLayer object and self.imgLogo.center is like 50 pixels up for refernce.
        CATransaction.setDisableActions(true)
        CATransaction.commit();
    }
}

【讨论】:

  • 错误地注释了初始化层方法,这就是引发崩溃的原因,现在它解决了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-18
  • 1970-01-01
  • 1970-01-01
  • 2015-02-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多