首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CABasicAnimation:从左到右

CABasicAnimation:从左到右
EN

Stack Overflow用户
提问于 2017-11-15 08:37:07
回答 1查看 826关注 0票数 0

通过下面的代码,我可以从左到右动画一个CALayer

代码语言:javascript
复制
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“(这是另一幅图像)将从左向右移动,就像以前的循环一样。

在这里,循环: 1 +循环: 2实际上是一个动画,我希望这个动画发生很多次。如何做到这一点?如能提供任何协助,将不胜感激。

EN

回答 1

Stack Overflow用户

发布于 2017-11-15 09:14:20

当您需要从左到右&反之亦然的动画,您也可以使用UIView.anima..动画。

代码语言:javascript
复制
     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)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47302753

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档