Ios 如何创建图像的侧边滚动效果?

Ios 如何创建图像的侧边滚动效果?,ios,swift,animation,uiimage,Ios,Swift,Animation,Uiimage,我想在我的应用程序的背景中添加一些图像(云),并用它们创建一个无休止的侧面滚动效果。他们不断地从左边跳到右边 我试图以这种方式实施: class AnimateClouds: UIImageView { override init(frame: CGRect) { super.init(frame: frame) } required init?(coder aDecoder: NSCoder) { super.init(coder: aD

我想在我的应用程序的背景中添加一些图像(云),并用它们创建一个无休止的侧面滚动效果。他们不断地从左边跳到右边

我试图以这种方式实施:

class AnimateClouds: UIImageView {
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        animateClouds()
    }


    func animateClouds() {
        UIView.animateWithDuration(0, delay:0, options: [.Repeat], animations: {
            self.center.x = self.center.x

            }) { (completed) -> Void in
                UIView.animateWithDuration(50, delay: 0, options: [.Repeat, .Autoreverse], animations: { () -> Void in

                    // self.center.x = self.center.x - (self.superview?.frame.size.width)! / 3

                    self.center.x = self.center.x + (self.superview?.frame.size.width)! //+ (self.frame.size.width) * 2
                    }, completion: { animationFinished in

                        self.removeFromSuperview()


                })
        }
    }
}

但我认为这看起来不太好,而且,我必须在我的故事板中创建很多云图像,并更改它们的自定义类。有什么更好的办法?我没有在Sprite Kit中制作此应用程序。

好的,您想无休止地从左向右移动云图像。当你想要有这样的动画时,写动画是很容易的,但是让动画看起来平滑和视觉效果好才是真正重要的

所以你是说在你的故事板中有多个云图像,这意味着所有这些云图像将出现在屏幕的不同位置(原点),这意味着当它们从左向右移动时,每个云图像从原点移动到屏幕最右端所需的时长是不同的

因此,不仅云图像动画的代码很重要,动画的持续时间也很重要。因此,您需要仔细计算持续时间,并为该持续时间量的云图像设置动画

这就是洛塔理论,让我们看一段代码片段,它达到了想要的动画效果

func animateCloud(cloud: UIImageView) {
//Assuming the cloud image will take 60 seconds to travel from
//left most point to the right most point of view controller's view
//and determining the speed at cloud travels
let cloudSpeed = 60.0/view.frame.size.width

//Since each cloud images are at different position (origin)
//The distance from each cloud to right end of screen will be different and
//The time taken (duration) by the each cloud to travel to right end of view will be different
let duration = (view.frame.size.width - cloud.frame.size.width) * cloudSpeed

UIView.animateiWithDuration(NSTimeInterval(duration), delay:0.0, options:.CurveLinear, animations: {
    cloud.frame.origin.x = self.view.frame.width
}, completion: {
      cloud.frame.origin.x = -cloud.frame.size.width
      self.animateCloud(cloud) // For endless animation
})
}

这应该能奏效。您可以添加任意数量的图像,然后调用animateCloud()方法来设置云图像的动画。

您的故事板中有云图像吗?或者你想以编程方式添加云图像?现在我的故事板中有这些图像。但我认为编程是解决这个问题的最好方法。你认为呢?如果你有太多的云图像,并且不想把故事板弄得乱七八糟,那么最好以编程的方式添加它。所以我看到的是你需要从左到右连续地移动云图像?是的。我希望它们出现在屏幕的左边缘之前,在右边缘之后消失。同时,我希望新的云不断弹出,以创建这种无休止的幻觉。我得到一个错误:“无法在“cloud.frame.origin.x=self.view.frame…”行上转换类型为“()->()”的值。”另一个问题:我通过视图控制器实现了这一点,并在viewDidLoad中调用了函数,或者创建了一个类,并更改了图像的自定义类?我能够成功地执行上述代码段,没有任何错误。如果出现错误,请使用以下内容替换动画块:动画:{()->Void in cloud.frame.origin.x=self.view.frame.width},这并不能解决我的问题。我一直需要创建大量的云图像来创建效果。有了这段代码,我无法创造出无穷无尽的云彩效果。我如何做到这一点,而不需要在我的故事板中创建云图像?您可以在ViewController的ViewDidDisplay中调用它。更改自定义图像类别是什么意思?您可以编辑上面的代码吗?我想我做错了什么。错误不断出现。