Skip to Content

iPhone Coding: Using the Slider

In terms of application development, the iPhone's UISlider is pretty standard. It works like nearly every other slider you've ever programmed. It offers a drag-able thumb control that moves between a minimum and maximum value. As its dragged, the slider produces events that you can redirect to your main application.

Creating the Slider

To create a slider, you allocate it and initialize it with a frame just like any other UIKit control:

slider = [[UISliderControl alloc] initWithFrame: CGRectMake(0.0f, 32.0f, 320.0f, 20.0f)];

Next, you specify its minimum and maximum values with the setMinValue: and setMaxValue: calls. The "setShowValue" call is optional. When set to YES, it displays the current value to the right of the slider (as shown here) so the user gets instant feedback.

[slider setMaxValue:100.0f];
[slider setMinValue:0.0f];
[slider setShowValue:YES];

Now you must add a target for events. Here, I tell the slider to issue the handleSlider: method for mouse dragged events. Fortunately, the iPhone seems to use standard NextStep event equivalents, so 7 corresponds to NSRightMouseDragged, 1 to NSLeftMouseDown and 2 to NSLeftMouseUp. Since the iPhone only supports one button, the right and left gets a little confused.

[slider addTarget:self action:@selector(handleSlider:) forEvents:7];

Next, add the behavior for the message that gets called. In this example, I just write the [slider value] to a text view.

[textView setText:[NSString stringWithFormat:@"End Value: %f", [slider value]]];

To finish up, I added a custom version of objc_msgSend_fpret that was floating around the 'net. Early versions of the iPhone toolchain (which includes most of the installed base at this time) do not support messages that return floating point values. This function adds that ability back. If you've installed version 0.20 of the SVN, you can omit this extra function.

Source

mainapp.m

SampleApp.h

SampleApp.m



Categories

How-tos iPhone

In terms of application development, the iPhone's UISlider is pretty standard. It works like nearly every other slider you've ever...
 

Add a Comment

*0 / 3000 Character Maximum

13 Comments

Filter by:
jjc


Tim, I've tried and tried but I Just can't fathom why you have such disagreement with Erica's posts. It can only be a positive thing for the site and the community. Is it really so painful to 1) look at the title 2) decide, "do I want to read this?", and c) if the answer is "no", SKIP PAST it??? I just don't get it. At the very least, the code samples could be on a different site and linked to, but to want to do away with her posts entirely because they are "off-topic" -- I just don't get it.

September 05 2007 at 12:48 PM Report abuse rate up rate down Reply
Jeem

In terms of judging writing I prefer the term "sucky" to describe using the phrase "in terms of".

September 05 2007 at 11:57 AM Report abuse rate up rate down Reply
Michael Rose

DJFriar:

http://www.tuaw.com/category/iphone

or

http://www.tuaw.com/bloggers/erica-sadun

Tim: I respect your point of view on the iPhone hacking posts (personally I wouldn't know a UIControl from a Uigur tribesman); however, there are some big reasons to have the iPhone development posts here --

a) We have Erica, and she has the skills.
b) We are the UNOFFICIAL Apple Weblog, and the iPhone hacking projects are completely unofficial. If you want to develop for the Mac you can go to developer.apple.com; there ain't no such animal for the iPhone. We can provide a service to the developer community and also give a taste of the hacker life to our larger audience.
c) Much of the iPhone indie development effort is taking place out of sight on IRC or on limited-access sites. We can provide a high-bandwidth, high-profile platform for the leading edge work.

September 04 2007 at 9:01 PM Report abuse rate up rate down Reply
R Muffet

While I understand those complaining of the technical nature of some of the iPhone posts, there is this:

Of today's TUAW titles appearing in the feed, this is about the only one that caught my interest. I don't use MarsEdit, Apollo, or iStat, and I'm half a planet away from being able to attend a Kentucky Apple Store opening. And the vapored Googlephone is only tangentially relevant to an Apple enthusiast,

Do I complain like a 2 year old that these don't suit my interests? No. They're just part of the feed, I don't have to read them if I'm not interested.

I appreciate Erica's iPhone 101s and think TUAW is really lucky to have someone of her skill on board. You can get the general Apple chit chat anywhere.

Also...

Get used to it, as I'm pretty sure tomorrow's next-gen iPod unveiling is going to see the release of OS X based iPods, and guess who's going to be one of the first hacking into them!

September 04 2007 at 8:42 PM Report abuse rate up rate down Reply
Andrew

kevin, insulting like a two year old ("Tim....Your dumb") is not a good way to conduct a conversation, is it? Although these iphone hacks are very cool, I don't believe they are the aimed at the target tuaw audience. tim does have some rational to separating the iphone hacks. no need to call him dumb.

September 04 2007 at 7:55 PM Report abuse rate up rate down Reply
tim

@kevin -

u somed up everyting about me

September 04 2007 at 7:55 PM Report abuse rate up rate down Reply
JD

Buckingham, they are bits of ObjectiveC / Cocoa code. You need to know how to program in order to make use of them. I tried, but the books I used were old and not only that, I never found a good way to understand WTF I'm doing and what I need to know. It's a very different way of doing things. Not only do you need to learn the Cocoa API, you need to learn ObjectiveC, which a different programming language from anything I've seen before.

September 04 2007 at 7:49 PM Report abuse rate up rate down Reply
Tom Craft

I just want to say tat while I love the tips and tricks, they are kinda hard to find when yo uwant to read an old one. So I would also like to see a dedicated location for all the iPhone stuff.

Or maybe just a specific keyword like "iPhoneCoding" so these posts are easy to find?

September 04 2007 at 7:39 PM Report abuse rate up rate down Reply
Buckingham

What are these? Do I enter the text in the post-it notepad?

September 04 2007 at 7:29 PM Report abuse rate up rate down Reply
Macintologist

I don't have an iPhone but I love these iPhone posts. If you don't like them don't read them.

September 04 2007 at 6:46 PM Report abuse rate up rate down Reply
Buy an ad here

Hot Apps on TUAW

Tweets

© 2012 AOL Inc. All Rights Reserved.