如何在swift中使用多个背景图像

时间:2016-07-19 06:51:44

标签: ios xcode swift2

func createBackgrounds() {
    for i in 0...2 {
       let bg = SKSpriteNode(imageNamed: "Background 1")
       bg.name = "BG"
       bg.zPosition = 0;
       bg.anchorPoint = CGPoint(x: 0.5, y: 0.5)
       bg.position = CGPoint(x: CGFloat(i) * bg.size.width, y: 0)
       self.addChild(bg)
    }
 }

我正在尝试将两个背景图像永久地循环,就像几分钟之后如果我想要更改背景1到2,那么我该如何为此编写代码?

1 个答案:

答案 0 :(得分:0)

我认为你最好在目标C中使用NSTimer,你可以像这样使用NSTimer:

NSTimer.scheduledTimerWithTimeInterval(10.00, target: self, selector: #selector(self.randomShowedImage), userInfo: nil, repeats: true)

然后使用CATransition设置转换以使其更加流畅,我曾设置过这样的转换:

  

let transition: CATransition = CATransition() transition.duration = 3.0 transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut) transition.type = kCATransitionFade self.bgImageView.layer.addAnimation(transition, forKey: nil)

如果我错了,请纠正我。我在swift中更新了答案