Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Fading out text on label

Hi everyone! I am currently working on a magic eight ball app that uses a lot of the same concepts Pasan covered in the Build a Simple iPhone App (with the random fact generator app). I am trying to add a little something different to it by allowing the user to shake the device to display a random answer based on an array. I was able to get everything working and hooked up, but I'm not sure how to best implement my next step: fading the text in and out. My goal is to have the user shake the device, have the text display and then fade out. The user should be able to shake the device repeatedly and have this process repeat as many times as desired. I wrote a repeat while loop to repeat the code based on the number of answers in my array. I also wrote an extension where I create the functions for fadeIn and fadeOut. The app does work as designed, but I'm not sure if this is the best way to implement this functionality.

As a quick summary, in the loop, I am setting my label randomAnswer equal to the random answer generated from a custom Struct with an list of possible answers. I then use the fadeIn and fadeOut functions written as extensions to make the text appear and disappear.

override func motionEnded(_ motion: UIEventSubtype, with event: UIEvent?) {

    var x = 0

    repeat {

        randomAnswer.text = answerRandom.generateRandomAnswer()
        self.randomAnswer.fadeIn()
        self.randomAnswer.fadeOut()
        x+=1

    } while x < 12

}

Thanks for any input! Feel free to ask for any additional snippets or info on my code!