1

Im trying to make a simple startup application to learn Swift. I am new to programming in general and I am in a little over my head. This bit of code is meant to highlight (change the background color) of a certain UIButton and then change it back. At first I added a delay of 0.5 seconds to each action but then I learned a IBAction only updates once at end of execution?. So I then added the delay to increment each time. The code works for the first 3 or 4 loops but then acts up and does not delay correctly. Please help, Thanks

func SSSays(clicks:Int) {

    sPattern.append(Int(arc4random_uniform(4) + 1))
    var wait = 0.5


    for button in sPattern {

        if(button == 1) {
            wait = wait + 0.5
            self.delay(wait) {self.BBB.backgroundColor = UIColor.init(red: (102/255), green: (178/255), blue: (255/255), alpha: (1))}

            wait = wait + 0.5
            self.delay(wait) { self.BBB.backgroundColor = UIColor.blueColor() }


        } else if(button == 2) {

            wait = wait + 0.5
            self.delay(wait) {self.RBB.backgroundColor = UIColor.init(red: (255/255), green: (153/255), blue: (153/255), alpha: (1))}

            wait = wait + 0.5
            self.delay(wait) {self.RBB.backgroundColor = UIColor.redColor() }


        } else if(button == 3){

            wait = wait + 0.5
            self.delay(wait) {self.GBB.backgroundColor = UIColor.init(red: (153/255), green: (255/255), blue: (204/255), alpha: (1)) }

            wait = wait + 0.5
            self.delay(wait) {self.GBB.backgroundColor = UIColor.greenColor() }


        } else if(button == 4){

            wait = wait + 0.5
            self.delay(wait) {self.OBB.backgroundColor = UIColor.init(red: (255/255), green: (204/255), blue: (255/255), alpha: (1)) }
            wait = wait + 0.5
            self.delay(wait) {self.OBB.backgroundColor = UIColor.magentaColor() }

        }
    }



}




func delay(delay:Double, closure:()->()) {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW,Int64(delay * Double(NSEC_PER_SEC))),dispatch_get_main_queue(), closure)
}

1 Answer 1

2

I suggest you look into animation functions. They were created for this reason and will greatly simplify your code. Here's a basic one that you can play with to get you started:

UIView.animateWithDuration(1.0, delay: 0.0, options: [], animations: {
            self.BBB.backgroundColor = UIColor.redColor()
            }, completion: nil)

Swift 4.0

UIView.animate(withDuration: 1.0, delay: 0.0, options:.allowAnimatedContent, animations: {
                self.BBB.backgroundColor = .red
            }, completion: nil)

You can then add in code for the completion block to handle additional code once that duration of the animation completes if you see fit.

options: [] can take parameters such as Autoreverse, Repeat, etc. to give you additional effects.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.