简体   繁体   中英

How to prevent lag when changing button title and color?

I am trying to have a button change title and color every second but there is lag. What happens is that when it changes from lets say YELLOW to RED the button will show R.. or simply ... for a split second. It does not happen every time the button changes, only a few times. This the the code I have.

@IBOutlet var tapButton: UIButton!

var color:[UIColor] = [UIColor.redColor(), UIColor.blueColor(), UIColor.yellowColor(), UIColor.greenColor()]

var colorName:[String] = ["RED", "BLUE", "YELLOW", "GREEN"]

func game(){
    timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(SecondViewController.subtractTime), userInfo: nil, repeats: true)
}

func subtractTime(){
    seconds -= 1

    let num1 = Int(arc4random_uniform(4))
    let num2 = Int(arc4random_uniform(4))

    tapButton.titleLabel?.textColor = color[num1]
    tapButton.setTitle(colorName[num2], forState: UIControlState.Normal)
}

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    game()
}

I have changed the segues to Show Details, this seems to have fixed my problem. I also have the timers stop when I change view controllers.

I no longer get the lag that I was experiencing before.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM