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
Share
Source: http://pastie.textmate.org/93816
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
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.
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 Permalink rate up rate down ReplyDJFriar:
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.
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!
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 Permalink rate up rate down Reply@kevin -
u somed up everyting about me
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 Permalink rate up rate down ReplyI 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?
What are these? Do I enter the text in the post-it notepad?
September 04 2007 at 7:29 PM Report abuse Permalink rate up rate down ReplyI 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 Permalink rate up rate down ReplyHot Apps on TUAW
Deals of the Day
more deals- Refurb Apple MacBook Air Laptops: 12" 64GB SSD for $699 + free shipping
- JVC Motion Sensing Clock Radio with Dual iPod Docks for $55 + free shipping
- Apple iPhone Headset with Mic for $4 + $2 s&h
- miFrame Picture Frame Dock for iPad for $64 + $8 s&h
- Refurb Apple iPod nano 8GB MP3 Player for $99 + free shipping, 16GB for $119
- Hannspree Apple-Shaped 28" 1080p LCD HDTV for $270 + free shipping
Software Updates
more updates- EFI Firmware Update brings Lion Internet Recovery to 2010-model Macs
- OS X Lion 10.7.3 released with Safari 5.1.3, Wi-Fi bug fix
- Aperture updated to 3.2.2, addresses Photo Stream issue
- Apple updates Keynote to address Lion issues
- Google Search app gets new look on iPad
- Apple releases Apple TV Software Update 4.4.3



13 Comments