【问题标题】:How to animate images running across the whole screen in swift如何快速动画在整个屏幕上运行的图像
【发布时间】:2018-04-04 15:50:47
【问题描述】:

所以我是 swift 的新手,我正在尝试制作一个有玩家汽车和对手汽车的汽车游戏。对手的汽车将穿过屏幕并重复,直到游戏结束。我现在正在实施对手汽车,但我遇到了一个问题,对手汽车的动画只穿过屏幕的一半,直到它们消失并再次开始循环。我将如何改变这一点,以便对手的汽车穿过整个屏幕?这是我当前对对手汽车的代码:

  //animation of opponent car1

        UIView.animate(withDuration: 4, delay: 0.0, options: [UIViewAnimationOptions.repeat], animations:
            {

                self.car1image.center.y += self.view.bounds.width



                }, completion: nil
        )


//opponent car2

        UIView.animate(withDuration: 4, delay: 1.0, options: [UIViewAnimationOptions.repeat], animations:
            {

                self.car2image.center.y += self.view.bounds.width


        }, completion: nil
        )

//opponent car3

        UIView.animate(withDuration: 4, delay: 2.0, options: [UIViewAnimationOptions.repeat], animations:
            {

                self.car3image.center.y += self.view.bounds.width


        }, completion: nil
        )

【问题讨论】:

    标签: swift animation uiview


    【解决方案1】:

    您正在将视图 width 添加到 y 位置。您应该添加视图 height。 (实际上您可能应该添加 screen 高度,而不是视图高度。)

    最好是做数学计算来计算汽车图像视图和屏幕底部之间的距离并添加该数量而不是添加视图/屏幕高度,因为添加全屏高度会使图像视图会越过屏幕底部,除非它从屏幕顶部的中心开始。

    【讨论】:

    • 如果我已经回答了你的问题,那么你应该接受我的回答。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多