将NSTIMER应用于Uilabel(Swift)



我是使用Swift的新手,目前正在从事一个小型学校项目。但是,我似乎无法奏效的一个功能是如何使NStimer在一定时间后更改Uilabel中的文本。我想将此功能应用于空白的uilabel(不愿意下),并且刚刚更改为显示消息的文本("您没有足够的点"),我希望将此消息删除,并且Uilabel为空白一秒钟左右。谁能帮我?谢谢。

class Upgrades: UIViewController{
    @IBOutlet weak var shprogresslabel: UILabel!
    @IBOutlet weak var fsprogresslabel: UILabel!
    @IBOutlet weak var shprogress: UIProgressView!
    @IBOutlet weak var fsprogress: UIProgressView!
    @IBOutlet weak var shbutton: UIButton!
    @IBOutlet weak var fsbutton: UIButton!
    @IBOutlet weak var storepoints: UILabel!
    @IBOutlet weak var cannotbuy: UILabel!
    @IBOutlet weak var label2: UILabel!
    @IBOutlet weak var label1: UILabel!
    var fsprice = Int()
    var shprice = Int()
    let defaults = NSUserDefaults.standardUserDefaults()
    func updateProgress1(){
        fsprogress.progress += 0.1
        let fsprogressvalue = self.fsprogress.progress
        fsprogresslabel.text = "(fsprogressvalue * 100)%"
    }
    func updateProgress2(){
        shprogress.progress += 0.2
        let shprogressvalue = self.shprogress.progress
        shprogresslabel.text = "(shprogressvalue * 100)%"
    }

    @IBAction func button2(sender: AnyObject) {
        if shprice <= 90{
            if SharingManager.sharedInstance.totalscore >= shprice{
                SharingManager.sharedInstance.shiphealth = SharingManager.sharedInstance.shiphealth + 1
                SharingManager.sharedInstance.totalscore = SharingManager.sharedInstance.totalscore - fsprice
                storepoints.text = String(SharingManager.sharedInstance.totalscore)
                shprice = shprice + 20
                print(shprice)
                label2.text = ("(shprice)")
                defaults.setInteger(SharingManager.sharedInstance.shiphealth, forKey: "SharingManager.sharedInstance.shiphealth")
                updateProgress2()
            }else{
                cannotbuy.text = "You dont have enough points"
            }
        }else{
            cannotbuy.text = "You have purchased all the available upgrades for this"
        }
    }
    @IBAction func button1(sender: AnyObject) {
        if fsprice <= 100{

            if SharingManager.sharedInstance.totalscore >= fsprice{
                    SharingManager.sharedInstance.bulletspeed = SharingManager.sharedInstance.bulletspeed - 0.15
                    SharingManager.sharedInstance.totalscore = SharingManager.sharedInstance.totalscore - fsprice
                    storepoints.text = String(SharingManager.sharedInstance.totalscore)
                    fsprice = fsprice + 10
                    print(fsprice)
                    label1.text = ("(fsprice)")
                    defaults.setDouble(SharingManager.sharedInstance.bulletspeed, forKey: "SharingManager.sharedInstance.bulletspeed")
                    updateProgress1()
            }else{
                cannotbuy.text = "You dont have enough points"
        }
        }else{
            cannotbuy.text = "You have purchased all the available upgrades for this"
        }
    }
    override func viewDidLoad() {
        if fsprice == 0{
            fsprice = 10
        }
        if shprice == 0{
            shprice = 10
        }
        if fsprice == 180{
            fsbutton.enabled = false
        }
        if shprice == 100{
            shbutton.enabled = false
        }
        storepoints.text = String(SharingManager.sharedInstance.totalscore)
        label1.text = ("(fsprice)")
        label2.text = ("(shprice)")

        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

有两种方法可以做到这一点:

使用NSTimer

@IBAction func button2(sender: AnyObject) {
    ...
    cannotbuy.text = "You don't have enough points"
    NSTimer.scheduledTimerWithTimeInterval(5, target: self, selector: #selector(ViewController.resetCannotBuy), userInfo: nil, repeats: false)
}
func resetCannotBuy() {
    cannotbuy.text = ""
}

使用dispatch_after

@IBAction func button2(sender: AnyObject) {
    ...
    cannotbuy.text = "You don't have enough points"
    let fiveSecondLater = dispatch_time(DISPATCH_TIME_NOW, 5 * Int64(NSEC_PER_SEC))
    dispatch_after(fiveSecondLater, dispatch_get_main_queue()) {
        self.cannotbuy.text = ""
    }
}

NSEC_PER_SEC是每秒纳秒的常数,等于10亿。

最新更新