Skip to Content

Autoblog reviews all the hottest cars
AOL Tech

Filed under: How-tos, iPhone

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

Click here to read all TUAW’s iPhone coverage

Reader Comments (Page 1 of 1)

Tip of the Day

To find out what version of Mac OS you are running, go to the Apple logo in the top left corner, click it and choose About This Mac. From that window you will see the version number, processor, memory and chosen startup disk. Clicking Software Update will check for updates, and More Info... will open up an extensive list of everything on your machine.


Follow us on Twitter!
TUAW [Cafepress]

Sponsored Links

Featured Galleries

DNC Macs
Macworld 2008 Keynote
Macworld 2008 Build-up
Google Earth for iPhone
Podcaster
AT&T Navigator Road Test
Bento for iPhone 1.0
Scrabble for iPhone
Tom Bihn Checkpoint Flyer Briefcase
Apple Vanity Plates
Apple booth Macworld 07
WorldVoice Radio
Quickoffice for iPhone 1.1.1
Daylite 3.9 Review
DiscPainter
Mariner Calc for iPhone
2009CupertinoBus
Crash Bandicoot Nitro Kart 3D
MLB.com At Bat 2009
Macworld Expo 2007 show floor
Apple Texas Hold 'Em

 

More Apple Analysis

AOL Radio TUAW on Stitcher