从左到右顺利重复动画

时间:2018-01-16 13:57:05

标签: ios swift uiview uiviewanimation

我正试图从左到右动画3个方格。当正方形从右侧消失时,正方形应从左侧重新出现。这个想法是正方形代表云,所以这个想法很明确。

这就是我现在所拥有的:

class ViewController: UIViewController {

    // contains the squares
    @IBOutlet weak var containerView: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        startAnimating()
    }

    func startAnimating() {
        UIView.animateKeyframes(withDuration: 10, delay: 0, options: .repeat, animations: {
            self.containerView.center.x += self.view.bounds.width

        }, completion: nil)
    }
}

结果如下:

clouds

正方形从左向右移动。这很好。问题是,一旦动画完成,方块就会从它们开始的地方重新出现。这不是一个平滑的连续运动,云从左向右移动,并再次从左侧慢慢再次出现。你是如何实现这一目标的?应该有第二个containerView吗?或者删除containerView并为单个云设置动画?

答案米兰诺萨解决了这个问题。但请注意:

  

我将containerView2放在containerView1上面与完全相同   约束(它们相互重叠)。只要确保IB没有   包含containerView2作为containerView1的子视图。

2 个答案:

答案 0 :(得分:3)

我想最简单的方法就是使用两个完全相同的容器。您可以使用完全相同的代码,并将containerView放在class ViewController: UIViewController { // contains the squares @IBOutlet weak var containerView: UIView! @IBOutlet weak var containerView2: UIView! override func viewDidLoad() { super.viewDidLoad() // this will transform the containerView2 to the left to appear as if it was exactly to the left of the containerView containerView2.transform = CGAffineTransform.identity.translatedBy(x: -self.view.bounds.width, y: 0) } override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) startAnimating() } func startAnimating() { UIView.animateKeyframes(withDuration: 10, delay: 0, options: .repeat, animations: { self.containerView.center.x += self.view.bounds.width self.containerView2.center.x += self.view.bounds.width }, completion: nil) } } 之上,以确保相互覆盖。

然后简单地使用:

containerView2

SELECT ticket_id, t.number, t.ticket_id, address, Ifnull(Concat(st.firstname, ' ', st.lastname), 'No Assignee') assigned, subject, t.created, t.updated ticket_updated, t.isoverdue FROM ost.`ost_ticket` t INNER JOIN ost.ost_ticket__cdata USING(ticket_id) LEFT JOIN ost.ost_user_email USING (user_id) LEFT JOIN ost.ost_user ou ON ou.id = t.user_id LEFT JOIN ost.ost_staff st USING (staff_id) WHERE t.status_id = '1' AND ( t.isanswered = 0 OR t.isoverdue = 1 ) 代表从左边回来的云。

答案 1 :(得分:0)

您可以尝试在启动动画之前将myList = ["paper", "Plastic", "aluminum", "PAPer", "tin", "glass", "tin", "PAPER", "Polypropylene Plastic"] 置于正确的位置。这样它首先出现在左侧。