【问题标题】:CABasicAnimation: From left to rightCABasicAnimation:从左到右
【发布时间】:2017-11-15 08:37:07
【问题描述】:

使用以下代码,我可以从左到右为 CALayer 设置动画

let imageLayer = CALayer()
imageLayer.frame = CGRect(x: 0, y: 500, width: 100, height: 70)
imageLayer.contents = UIImage(named: "first_image")?.cgImage

let animation = CABasicAnimation()
animation.keyPath = "position.x"
animation.fromValue = 100
animation.toValue = 300
animation.duration = 3
animation.fillMode = kCAFillModeForwards
animation.isRemovedOnCompletion = true
animation.repeatCount = 2
imageLayer.add(animation, forKey: "basic")

self.view.layer.addSublayer(imageLayer)

我想做的是,在

循环:1 - “first_image”将从左向右移动

循环:2 - “second_image”(这是另一张图片)将从左侧移动 像上一个周期一样向右。

这里 Cycle: 1 + Cycle: 2 实际上是一个动画,我希望这个动画发生多次。如何做到这一点? 任何帮助将不胜感激。

【问题讨论】:

    标签: ios swift cabasicanimation


    【解决方案1】:

    由于您需要从左到右,反之亦然动画,您也可以使用UIView.anima.. 进行动画。

         let testViewTowardsLeft = UIView(frame: CGRect(x: 0, y: 150, width: 50, height: 50))
        testViewTowardsLeft.backgroundColor = .red
        view.addSubview(testViewTowardsLeft)
        UIView.animate(withDuration: 1, delay: 0, options: .repeat, animations: {Bool in
            testViewTowardsLeft.frame = CGRect(x: 100, y: 150, width: 50, height: 50)
    
        }, completion: nil)
    
    
        let testViewTowardsRight = UIView(frame: CGRect(x: 100, y: 100, width: 50, height: 50))
        testViewTowardsRight.backgroundColor = .red
        view.addSubview(testViewTowardsRight)
        UIView.animate(withDuration: 1, delay: 0, options: .repeat, animations: {Bool in
            testViewTowardsRight.frame = CGRect(x: 0, y: 100, width: 50, height: 50)
    
        }, completion: nil)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-18
      • 2022-01-13
      • 2017-07-22
      • 2016-05-02
      • 2016-03-01
      • 2012-01-18
      • 2016-05-17
      相关资源
      最近更新 更多