如何用计时器显示图像

时间:2016-04-14 12:36:36

标签: ios swift

我希望将图像显示3.0秒,并在3秒后关闭。

我写的代码可以做到这一点,但它对我不起作用,我想知道为什么并且有解决方案来解决我的问题。

主要场景:

 var myTimer: NSTimer?
   var imageView: UIImageView?


    var IsShowing = false

    override func didMoveToView(view: SKView) {

            let defaults = NSUserDefaults.standardUserDefaults()
            if defaults.stringForKey("isAppAlreadyLaunchedOnce") != nil{
                print("App already launched")

                //Do Somthing
            } else {
                defaults.setBool(true, forKey: "isAppAlreadyLaunchedOnce")
                myTimer = NSTimer.scheduledTimerWithTimeInterval(
                    3.0, target: self, selector: #selector(MainScene.SetImage), userInfo: nil, repeats: false)
                backgroundImage.hidden = false
                print("App First launched")
            }


            backgroundImage = SKSpriteNode(imageNamed: "Image")
            backgroundImage.position = CGPointMake(self.size.width/2, self.size.height/2)
            backgroundImage.size = CGSizeMake(self.size.width, 580)
            addChild(backgroundImage)


}

      func SetImage() {

    backgroundImage = SKSpriteNode(imageNamed: "Image")
    backgroundImage.position = CGPointMake(self.size.width/2, self.size.height/2)
    backgroundImage.size = CGSizeMake(self.size.width, 580)
    addChild(backgroundImage)


    if IsShowing == true {
        backgroundImage.hidden = false
    }else {
       backgroundImage.hidden = true
    }

}

2 个答案:

答案 0 :(得分:1)

要启动计时器,您需要myTimer.fire(),但self.performSelectorAfterDelay也适用于此情况

 override func didMoveToView(view: SKView) {

        let defaults = NSUserDefaults.standardUserDefaults()

        if defaults.stringForKey("isAppAlreadyLaunchedOnce") != nil{
            print("App already launched")
        } 
        else {

            backgroundImage = SKSpriteNode(imageNamed: "Image")
            backgroundImage.position = CGPointMake(self.size.width/2, self.size.height/2)
            backgroundImage.size = CGSizeMake(self.size.width, 580)
            addChild(backgroundImage)

            defaults.setBool(true, forKey: "isAppAlreadyLaunchedOnce")

            self.performSelector(#selector(MainScene.hideImage), withObject: nil, afterDelay: 3.0)
            print("App First launched")
       }
}

  func hideImage() {

    backgroundImage.hidden = true

}

或使用您当前的版本

override func didMoveToView(view: SKView) {

        let defaults = NSUserDefaults.standardUserDefaults()
        if defaults.stringForKey("isAppAlreadyLaunchedOnce") != nil{
            print("App already launched")

            //Do Somthing
        } else {
            defaults.setBool(true, forKey: "isAppAlreadyLaunchedOnce")
            myTimer = NSTimer.scheduledTimerWithTimeInterval(
                3.0, target: self, selector: #selector(MainScene.SetImage), userInfo: nil, repeats: false)
            myTimer.fire() // <----- Add this


            backgroundImage.hidden = false // this probably does nothing
            print("App First launched")
        }


        backgroundImage = SKSpriteNode(imageNamed: "Image")
        backgroundImage.position = CGPointMake(self.size.width/2, self.size.height/2)
        backgroundImage.size = CGSizeMake(self.size.width, 580)
        addChild(backgroundImage)

        backgroundImage.hidden = true // seems to hide the image as soon as it's created
}

答案 1 :(得分:1)

你不需要定时器,这可以通过简单的GCD调用来实现。

override func didMoveToView(view: SKView) {

    let defaults = NSUserDefaults.standardUserDefaults()
    if defaults.stringForKey("isAppAlreadyLaunchedOnce") == nil {
        defaults.setBool(true, forKey: "isAppAlreadyLaunchedOnce")   
        displayBackgroundImageForSeconds(3)
        print("App First launched")
    }

}

func displayBackgroundImageForSeconds(seconds: Int) {
    backgroundImage = SKSpriteNode(imageNamed: "Image")
    backgroundImage.position = CGPointMake(self.size.width/2, self.size.height/2)
    backgroundImage.size = CGSizeMake(self.size.width, 580)
    addChild(backgroundImage)   
    let time = dispatch_time(DISPATCH_TIME_NOW, Int64(seconds) * Int64(NSEC_PER_SEC))
    dispatch_after(time, dispatch_get_main_queue(), { backgroundImage.hidden = true })           
}