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 trialRomesh Singhabahu
1,195 PointsInstead of click a button to show facts...how can you implement a way t show it automatically?
Hi all...
I want to make instead of a button that show the random fact...want them show auto like when you see a web page and see a coursel effect.
I tried to understand how it works but dind't find the answer...
I followed the Amit previus beginner course and if is possible to add a gesture by shake the facts labels...
Can you please show me the code to implement and where in the demo app?
Thanks a lot
Romesh
3 Answers
Daniel Reedy
25,284 PointsTimer Feature
You'll want to look at the NSTimer Class Rreference for anything dealing with timers.
The method you'll want to read up on is +scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:
and to implement it in this app you can add one line to your viewDidLoad()
method in the ViewController.swift file:
NSTimer.scheduleTimerWithTimeInterval(3.0, target: self, selector: Selector("showFunFact"), userInfo: nil, repeats: true)
I've used 3.0
as the interval, meaning it switches out every 3 seconds.
Shake Feature
To implement the shake to load fact feature you will again add a few things to the ViewController.swift file.
First, make the view able to become the first responder by adding:
override func canBecomeFirstResponder() -> Bool {
return true
}
Then, make it the first responder by adding this line to the viewDidLoad()
method:
self.becomeFirstResponder()
Last, implement the code to handle the shake gesture by overriding motionEnded:withEvent:
method:
override func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent) {
if(event.subtype == UIEventSubtype.MotionShake) {
showFunFact()
}
}
Romesh Singhabahu
1,195 PointsThanks @Daniel Reedy ...now I try and let you know... Thanks again
Romesh Singhabahu
1,195 Pointswhen they say (apple doc) "argument" do they mean also even the declaration of a label for example in this case of the demo the: funFactLabel
?
override func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent) { if(event.subtype == UIEventSubtype.MotionShake) { showFunFact() } }
Can even be applied to a button? for example the refresh button?
Thanks
Cheers
Romesh Singhabahu
1,195 PointsRomesh Singhabahu
1,195 PointsThanks Daniel...Know I got it...I works perfectly...thanks again... Last thing related to the same issue:
in this code I implemented a fade in effect and the random effect but in the case I don't want anymore the button because I put the "shake gesture" those line I should re-write under the >viewDidLoad() is correct?
Thanks again
Cheers
Romesh