Audio

Anything related to my audio and music work will be in here, including anything related to ProTools or Logic Pro

Life

Anything relating to general life, everything in this section is my own views

Software Development

Anything related to my development work on iPhone SDK, OS X, using objective-c, cocoa, and cocoa touch.

University

Anything related to my university course, work, and dissertation. Anything expressed in this section is my own thoughts on the matter.

Web Development

Anything related to my web development work, updates related to this site, or anything web related

Home » Software Development

iPhone App in a day – Warm-Up Weights

Submitted by Adam Boardman on Sunday, 7 February 2010No Comment

After spending a while sorting out my MacBook Pro with all my audio software I needed a break from that so decided to challenge myself into building an iPhone application that I personally would find useful, and would use a lot. This app would have to be written and fully working in one day (24 hours) from start to finish whatever I decided to build. It so happened that the app I built today took just under an hour to build to the point I wanted it at, so I’ll use the remaining 23 hours to pretty it up later in the month.

I came up with a few ideas along the way but something I’m constantly needing while at the gym is a weight calculator for my warm-up sets. Normally I would use the built-in calculator in iPhone OS3 and workout each weight individually as I needed to, so I thought that surely it would be easier and quicker to have all those individual weights in one place, and only have to enter one weight for them all to be calculated at the same time.

Working out these weights is a straight forward enough process as they are just percentages of the max weight I managed the previous session starting at 50%, and increasing 10% up to 90%. I needed this displayed in a logical way, and decided on the 100% weight being at the top, and then the warm-up weights going down the screen from 50% to 90%, with a button at the bottom of the list to make the magic happen so to speak.

I fired up Xcode and got to work on this little utility app. I am so used to developing using the cocos2d-iPhone framework that I accidentally fired up one of those templates and had to go back and start again with a standard View-based Application. This would require me to actually make use of Interface Builder which I’ve not used since I first looked at iPhone development back in November whilst working through the “Beginning iPhone 3 Development: Exploring the iPhone SDK” book, which is fantastic by the way!

First stop for me was building the UI for the app so I fired up IB and got to it. The app uses one text field, some labels, and a round rect button so as far as interfaces go, this is pretty simple. Some labels are there specifically just to indicate weight percentages, whereas others hold the values of the weights and only become visible when the user has input their 100% weight.

As you can see in the screen shot it’s not the prettiest looking thing in the world, however it does the job it’s meant to and that’s what I care about at the moment. If your an iPhone developer you may have noticed the UIView has been changed to a UIControl which allows actions, that allows me to fire off stuff from touches to the view.

So that’s the interface built and ready, now for the coding of the actual thing. I spent about 10min thinking about what actually needed to happen to get everything to happen at the same time, I didn’t want to have one value update, then the next, then the next, and so on, I wanted everything to happen at the touch of the “Find Weights” button. This needed one utility method and a main method which did the updating, which wasn’t too much work to muddle through.

-(void)udpateLabels {

 float full = [fullField.text intValue];

 if (fullField.text.length > 0) {
 fiftyLabel.text = [[NSString alloc] initWithFormat:@"%2.1f", [self findPercent:50 :full]];
 sixtyLabel.text = [[NSString alloc] initWithFormat:@"%2.1f", [self findPercent:60 :full]];
 seventyLabel.text = [[NSString alloc] initWithFormat:@"%2.1f", [self findPercent:70 :full]];
 eightyLabel.text = [[NSString alloc] initWithFormat:@"%2.1f", [self findPercent:80 :full]];
 ninetyLabel.text = [[NSString alloc] initWithFormat:@"%2.1f", [self findPercent:90 :full]];
 }
}

-(float)findPercent:(int)percent : (float)full {
 float divide = full / 100;
 float result = divide * percent;

 return result;
}

The code for these methods is pretty simple, I first set a floating point to the contents of the text field then using an if statement check that a value is present, if there is no value then I do nothing and nothing is changed on screen, if however there is a value in the box then that value is run through the findPercent method. This utility method just runs the calculations on the 100% value to get back the value of the given percentage, for example if the percentage was 60% the method would look like this.

-(float)findPercent:60 percent : (float)full {
 float divide = full / 100;
 float result = divide * 60;

 return result;
 }

Doing things this way enables everything to happen at once as far as the user is aware, however the program will actually run through the if statement until each of the values is filled.

I’ve already found a few things I’m going to change with this app even though I’ve just built it and they are, in no particular order -

  • Custom keyboard with a decimal point as the current numeric keyboard doesn’t offer one
  • An alert window if the user hasn’t input a value for 100% but clicks the button
  • Possibly removing the button completely and running the updateLabels method on the keyboard closing
  • Much prettier UI

Popularity: 13% [?]

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.