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


![TUAW [Cafepress]](http://www.blogsmithmedia.com/www.tuaw.com/media/tuaw-cafepress-promo.png)


Reader Comments (Page 1 of 1)
tim said 6:21PM on 9-04-2007
this is why we REALLY need a separate iphone site. if you are doing this, why not regular cocoa/carbon development notes?
how does this qualify as news/rumors/general apple info? again, its just another 'i can do this' post by you know who.
Reply
Justin said 6:32PM on 9-04-2007
tim, show me where you can find this online elsewhere and maybe i'll agree with you.
i, for one, am glad that someone is out there teaching the programmers how to make sweet apps for my iphone. if apple had released a kit for this then i'd agree with you; but for now the 'i can do this; and now you can too' posts from erica are greatly welcomed.
Reply
tim said 6:39PM on 9-04-2007
@justin -
this has nothing to do with whether or not you can find it somewhere else or not, the point is that they should make a dedicated place to find it. as far as i know, this has hardly ever been a developer website, i even remember old posts from david and scott saying they are anything but developers (citations?)...
i say if they do this, why not other mac programming? its out of place here unless they make it more common somehow.
Reply
Stephen Flynn said 6:49PM on 9-04-2007
I don't have an iPhone but I love these iPhone posts. If you don't like them don't read them.
Reply
Buckingham said 7:29PM on 9-04-2007
What are these? Do I enter the text in the post-it notepad?
Reply
DJFriar said 7:47PM on 9-04-2007
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?
Reply
JeffDM said 7:49PM on 9-04-2007
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.
Reply
tim said 7:55PM on 9-04-2007
@kevin -
u somed up everyting about me
Reply
coolio said 7:57PM on 9-04-2007
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.
Reply
Reg said 8:42PM on 9-04-2007
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!
Reply
Michael Rose said 9:01PM on 9-04-2007
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.
Reply
King Fysel said 11:57AM on 9-05-2007
In terms of judging writing I prefer the term "sucky" to describe using the phrase "in terms of".
Reply
jjc said 12:48PM on 9-05-2007
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.
Reply