Articles tagged with iPad

Swift timer

This is a simple timer, using Swift.

var theTimer = NSTimer()

//function to start the timer (if needs be)
func startTimer() {
  //start the timer with a 5 second delay
  self.theTimer = NSTimer.scheduledTimerWithTimeInterval(5, target: self, selector: Selector("timerIsDone"), userInfo: nil, repeats: false)

func stopTimer() {
  //start the timer with a 5 second delay  

//The function called when the timer is done
func timerIsDone() {
  println("Do something")

//start the timer - this can be in called as needed from another function or an init
Read post... Posted in 'Programming'

Flipping a UIImage

Flipping an image using objective-c is so easy once you know how...

//Create a normal image
UIImage* img = [UIImage imageNamed:@"myImage.png"];
//Flip it
UIImage* flippedImage = [UIImage imageWithCGImage:img.CGImage scale:1.0 orientation:  UIImageOrientationDownMirrored];

That's it! Now to have it as an method we can use more conveniently, try this:

-(UIImage *)flipImage:(UIImage *)img {
    UIImage* flippedImage = [UIImage imageWithCGImage:img.CGImage scale:1.0 orientation:  UIImageOrientationDownMirrored];
    return flippedImage;

Then we can send any image to it and it will return a flipped version.

Read post... Posted in 'Programming'

Basics of the NSNotificationCenter

The uses of NSNotificationCenter can be many; It is handy for using to notify the app that a background download of data, a post of some kind is done, or some calculation is finished. It can also be useful for letting sub views know when some root level functions happen in the app such as shutdown/sending to background because the user tapped the home button. In this example I will be doing just that.

In the AppDelegate you should find a method named:

- (void)applicationDidEnterBackground:(UIApplication *)application

This is called when the app starts the process to pause into the background state. Since this method can only be called by the app delegate and not a sub view notifications are ideal for subviews so that they can do what they need such as save data or stop heavy processing of data.

- (void)applicationDidEnterBackground:(UIApplication *)application{
   [[NSNotificationCenter defaultCenter] postNotificationName:@"UIApplicationDidEnterBackgroundNotification" object:self];

'UIApplicationDidEnterBackgroundNotification' is the name of the notification we send out. If we name notifications in a smart way it makes sense when ...

Read post... Posted in 'Programming'