Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 08-09-2008, 09:57 PM   #1 (permalink)
New Member
 
Join Date: Aug 2008
Posts: 6
Capt Greenbean is on a distinguished road
Question Spin a UIPicker on button press

I am attempting to write an iPhone application that makes unusual use of the UIPicker. Specifically, I am attempting to load a limited number of items into the picker an then, when the user presses a button, have the picker spin and land on a random selection... An example of what I am trying to achieve can be seen in the latest offering from Urban Spoon in the Apt Store...

Does any one have an idea on the best way to accomplish the spinning effect?

Thank you...
Capt Greenbean is offline   Reply With Quote
Old 08-10-2008, 01:38 AM   #2 (permalink)
New Member
 
Join Date: Apr 2008
Posts: 420
jeff_lamarche is an unknown quantity at this point
Send a message via AIM to jeff_lamarche Send a message via Yahoo to jeff_lamarche
Default

This is actually really easy - I did this in one of the sample apps in my book, where we implemented a simple slot machine game.

All you need to do is:

Quote:
[picker selectRow:newValue inComponent:i animated:YES];
[picker reloadComponent:i];
In the code above, i is the component (0 if you have only 1), and newValue is the row number to be selected.
__________________
Check out my iPhone Dev Blog
You can send me e-mail at my forum username at mac dot com.
jeff_lamarche is offline   Reply With Quote
Old 08-10-2008, 08:28 AM   #3 (permalink)
New Member
 
Join Date: Aug 2008
Posts: 6
Capt Greenbean is on a distinguished road
Smile

Thanks Jeff!

I was trying to make this much more complicated ;-)

I can't test this right now, but I will a little later today and report back on how it works...
Capt Greenbean is offline   Reply With Quote
Old 08-12-2008, 10:49 PM   #4 (permalink)
New Member
 
Join Date: Aug 2008
Posts: 6
Capt Greenbean is on a distinguished road
Thumbs up

I apologize for not getting back on this sooner... Jeff's suggestion worked like a charm! Thank you again!

Now, if I can just figure out how to make my UIPicker "spin" continuously for a few moments before displaying the result I'll be all set...

Jeff, any ideas on that last part?
Capt Greenbean is offline   Reply With Quote
Old 08-12-2008, 10:52 PM   #5 (permalink)
New Member
 
Join Date: Apr 2008
Posts: 420
jeff_lamarche is an unknown quantity at this point
Send a message via AIM to jeff_lamarche Send a message via Yahoo to jeff_lamarche
Default

Well, I had a couple of ideas, but haven't tried any of them. One idea was to have additional dummy rows in the table - I was thinking of repeating the values for a while. The other thought was to subclass UIPickerView and tweak the animation settings. The latter is cleaner, I suspect the former is a lot easier...
__________________
Check out my iPhone Dev Blog
You can send me e-mail at my forum username at mac dot com.
jeff_lamarche is offline   Reply With Quote
Old 01-14-2009, 06:03 AM   #6 (permalink)
New Member
 
Join Date: Jan 2009
Location: Medellín, Colombia
Posts: 1
betancur is on a distinguished road
Default

Capt Greenbean, did you figure out how to do it?
betancur is offline   Reply With Quote
Old 01-28-2009, 12:42 PM   #7 (permalink)
New Member
 
Join Date: Sep 2008
Posts: 9
kylehayes is on a distinguished road
Exclamation

I would like to know this as well, as I have a business requirement for this same type of thing. Especially like how UrbanSpoon even blurs the entries upon spinning.

And Jeff, the book you did with Dave was one of the best programming books I have ever read and is the only reason I understand iPhone development as well as I do today.
kylehayes is offline   Reply With Quote
Old 01-28-2009, 01:47 PM   #8 (permalink)
New Member
 
Join Date: Apr 2008
Posts: 420
jeff_lamarche is an unknown quantity at this point
Send a message via AIM to jeff_lamarche Send a message via Yahoo to jeff_lamarche
Default

Kyle:

Thanks - I appreciate you saying that about the book.

As for spinning a picker, I haven't actually tried to do what they did in Urban Spoon - our book example just spins once, but my guess is that their picker datasource "lies" about the number of rows, saying there are more than there really are, so let's say there are 10 rows, the data source might say there are 100 rows in the component (actual count multiplied by 10 - just picking a multiplier out of the air), then when the component asks for a row, it just calculates the requested row number modulus the actual row count to determine the row to feed, so in our case, when it asks for row 31, 31%10 = 1, so it feeds row 1. That way, you would have the illusion of many rows, but it's really just the same rows over and over again.

As for the "blur" effect, I'm guessing that the data source is not supplying strings, it's suppling views, and I'm guessing it's creating an imageview based on the text that would be there and supplies that when it's spinning. That's just a guess, though, I don't know for sure, and I haven't tried it myself.
__________________
Check out my iPhone Dev Blog
You can send me e-mail at my forum username at mac dot com.
jeff_lamarche is offline   Reply With Quote
Old 01-28-2009, 02:51 PM   #9 (permalink)
New Member
 
Join Date: Sep 2008
Posts: 9
kylehayes is on a distinguished road
Default

So check this out, I looked at the UrbanSppon app a little more in detail, and the first two components seemingly have an infinite amount of data. If you scroll up to the A's, you will see that there is no white space above it and it immediately shows the end of the alphabet again.

I tried to keep going for a while to see if maybe they just had a massive amount of data but to no avail. It just seemed to loop over and over again. Pretty clever this little app is.
kylehayes is offline   Reply With Quote
Old 01-28-2009, 02:54 PM   #10 (permalink)
New Member
 
Join Date: Sep 2008
Posts: 9
kylehayes is on a distinguished road
Default

Just saw this on another post:
UIPickerView act as spinning wheel - Page 2 - iDevKit
kylehayes is offline   Reply With Quote
Old 01-28-2009, 02:55 PM   #11 (permalink)
New Member
 
Join Date: Apr 2008
Posts: 420
jeff_lamarche is an unknown quantity at this point
Send a message via AIM to jeff_lamarche Send a message via Yahoo to jeff_lamarche
Default

They probably set the row at the count of objects + 1, and when the picker gets set to a number below count, they probably switch it to a higher number and don't animate the change, same thing if they go above the highest number..

Again, I'm just guessing...
__________________
Check out my iPhone Dev Blog
You can send me e-mail at my forum username at mac dot com.
jeff_lamarche is offline   Reply With Quote
Old 01-28-2009, 03:47 PM   #12 (permalink)
New Member
 
Join Date: Sep 2008
Posts: 9
kylehayes is on a distinguished road
Default

Jeff your modulus thought worked perfectly! It goes forever, combined with the following code to actually spin the thing:

- (IBAction)spinTheSpinner
{
NSRunLoop *myRunLoop = [NSRunLoop currentRunLoop];
// Create and schedule the first timer.
NSDate* futureDate = [NSDate dateWithTimeIntervalSinceNow:0.0];
myTimer = [[NSTimer alloc] initWithFireDate:futureDate
interval.2)
target:self
selector:@selector(updateLogs)
userInfo:nil
repeats:YES];
[myRunLoop addTimer:myTimer forMode:NSDefaultRunLoopMode];


}
- (void)updateLogs{
if(topdownSpin == 0){
[spinner selectRowfakeRowCount-1) inComponent:0 animated:YES];
topdownSpin = 1;
}else{
[spinner selectRow:0 inComponent:0 animated:YES];
topdownSpin = 0;
}

[spinner reloadComponent:0];

}
kylehayes is offline   Reply With Quote
Old 01-29-2009, 02:38 PM   #13 (permalink)
New Member
 
Join Date: Apr 2008
Posts: 420
jeff_lamarche is an unknown quantity at this point
Send a message via AIM to jeff_lamarche Send a message via Yahoo to jeff_lamarche
Default

Anyone interested in this thread might like this

iPhone Development: Longer Spinning & Blurring

It has sample code.
__________________
Check out my iPhone Dev Blog
You can send me e-mail at my forum username at mac dot com.
jeff_lamarche is offline   Reply With Quote
Old 01-29-2009, 03:21 PM   #14 (permalink)
New Member
 
Join Date: Sep 2008
Posts: 9
kylehayes is on a distinguished road
Default

Awesome! Reading it right now. I like your approach so far.
kylehayes is offline   Reply With Quote
Old 09-11-2009, 01:23 AM   #15 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 4
KyleLuchinski is on a distinguished road
Default I know im behind

hey jeff....im having a little trouble with this still. First of all, your book is excellent, beats the books from CS classes in college. However, my app closes when i press my button that spins my UIPicker. Below is my spin method. any ideas as to why its not spinning, and just closing out in the iphone simulator?

int numInRow = 1;
int lastVal = -1;
for (int i = 0; i < 5; i++) {
int newValue = random() % [self.names count];

if(newValue == lastVal)
numInRow ++;
else
numInRow = 1;

lastVal = newValue;

[triplePicker selectRow:newValue inComponent:i animated:YES];
[triplePicker reloadComponent:i];


NSString *title = [[NSString alloc] initWithFormat:
@"Hello %@", names];
NSString *message = [[NSString alloc] initWithFormat:
@"%@ is Awesome", names];


UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];


[alert show];
[alert release];
[title release];
[message release];
KyleLuchinski is offline   Reply With Quote
Old 06-21-2011, 08:48 AM   #16 (permalink)
Registered Member
 
Join Date: Jun 2011
Posts: 3
mark.ed is on a distinguished road
Default

I'm working on a similar app at the moment that uses 3 picker wheels to play three sounds. It can be spun manually and works fine, but I'm trying to get it working with a random button.

I'm using arc4random() to spin the wheels to random, and I've got it to display an alert message with the random selection (by adding a 0.2 delay time), but the problem is that I can't get it to play any sounds from random.

If I have manually selected rows before hitting the random button then it will play the last manually selected sounds, but never the randomly selected sounds.


Code:
- (void)firstColumn {
    switch (whatSound) {
        case 0: // do nothing
            break;
        case 1: // do nothing [select]
            break;
        case 2: AudioServicesPlaySystemSound(bestSoundID);
            break;
        case 3: AudioServicesPlaySystemSound(bigMoustacheSoundID);
///////more in here/////
        default: // do nothing
        break;    
    }
}

- (void)secondColumn {
    switch (secondSound) {
        case 0: // do nothing
            break;
        case 1: // do nothing [select]
            break;
        case 2: AudioServicesPlaySystemSound(bestSoundID);
            break;
        case 3: AudioServicesPlaySystemSound(bigMoustacheSoundID);
            break;
///////more in here////////
        default: // do nothing
        break;    }
}

- (void)thirdColumn {
    switch (thirdSound) {
        case 0: // do nothing
            break;
        case 1: // do nothing [select]
            break;
        case 2: AudioServicesPlaySystemSound(bestSoundID);
            break;
        case 3: AudioServicesPlaySystemSound(bigMoustacheSoundID);
            break;
/////////more in here//////////
        default: // do nothing
        break;    }
}

/////normal play action in here//////

- (IBAction)randomAction
{
    {
        [triplePicker selectRow:arc4random() % [firstHalf count] inComponent:kFirstComponent animated:YES];
        [triplePicker selectRow:arc4random() % [secondHalf count] inComponent:kSecondComponent animated:YES];
        [triplePicker selectRow:arc4random() % [thirdHalf count] inComponent:kThirdComponent animated:YES];
        
    }
        {
            [self performSelector:@selector(messageMe) withObject:nil afterDelay:(0.2)];
            }
    {
        [self performSelector:@selector(randomPlay) withObject:nil afterDelay:(1)];
         }
}

- (void)pickerView:(UIPickerView *)triplePicker didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
	
    if (component==kFirstComponent) {
        whatSound = row +1;
    } else {
        if (component==kSecondComponent) {
            secondSound = row +1;
        } else {
            if (component==kThirdComponent) {
                thirdSound = row +1;
            }
        }
    }
}

- (void)messageMe {

        NSInteger firstRow = [triplePicker selectedRowInComponent:kFirstComponent];
        NSInteger secondRow = [triplePicker selectedRowInComponent:kSecondComponent];
        NSInteger thirdRow = [triplePicker selectedRowInComponent:kThirdComponent];
        
        NSString *first = [firstHalf objectAtIndex:firstRow];
        NSString *second = [secondHalf objectAtIndex:secondRow];
        NSString *third = [thirdHalf objectAtIndex:thirdRow];
        
        NSString *message = [[NSString alloc] initWithFormat:
                             @"%@ %@ %@.", first, second, third];
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:
                              @"Dr Belond Says:"
                                                        message:message
                                                       delegate:nil
                                              cancelButtonTitle:@"I Love You!!"
                                              otherButtonTitles:nil];
        [alert show];
        [alert release];
        [message release];

}

- (void)playMe {
    [self firstColumn];
        [self performSelector:@selector(secondColumn) withObject:nil afterDelay:(1)];
        [self performSelector:@selector(thirdColumn) withObject:nil afterDelay:(2)];
}
Has anyone come across a problem like this? It seems like it doesn't acknowledge the selected row to play the sound unless it's manually selected, but still acknowledges it to display the correct alert.

If anyone can help, please do!

Last edited by mark.ed; 06-21-2011 at 08:04 PM.
mark.ed is offline   Reply With Quote
Old 02-16-2012, 08:35 AM   #17 (permalink)
Registered Member
 
Join Date: Feb 2012
Posts: 1
vickysoniji is on a distinguished road
Default Sample code URL not working...

Could you please check sample code URL ? Its not working..... Is it possible for you to send new URL ?

Quote:
Originally Posted by jeff_lamarche View Post
Anyone interested in this thread might like this

iPhone Development: Longer Spinning & Blurring

It has sample code.
vickysoniji is offline   Reply With Quote
Reply

Bookmarks

Tags
random, spin, uipicker

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 326
9 members and 317 guests
chiataytuday, coolman, givensur, ipodphone, jbro, mottdog, mtl_tech_guy, Punkjumper, vilisei
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,649
Threads: 94,114
Posts: 402,882
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Anwerbl
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 09:19 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0