Roll Over UIPickerView Animation [selectRow: inComponent: animated:]

I use UIPickerView to display random numbers. The user can click a button and thus cause a random number selection inside the UIPickerView .

It doesn't matter how many objects or numbers are displayed inside the UIPickerView when I call the method:

 [self.picker selectRow:randomRow inComponent:0 animated:YES]; 

It is always animated at the same time interval.

Is there any option or way to extend the animation time interval of the above method?

I tried putting it in an animation block:

 [UIView beginAnimations:@"identifier" context:nil]; // code [UIView commitAnimations]; 

but it seems to be a dead end.

I also tried to execute it in completion blocks:

 // 0.34f is the approximate defualt apple animation [UIView animateWithDuration:0.34f animations:^{ [self.picker selectRow:randomRow inComponent:0 animated:YES]; } completion:^(BOOL finished) { [UIView animateWithDuration:0.34f animations:^{ [self.picker selectRow:randomRow inComponent:0 animated:YES]; } completion:nil]; }]; 

Any help would be appreciated.

+7
ios animation uipickerview
source share
3 answers

I made a project and played for a while until I found out this difficult decision. It is based on the performSelector:afterDelay

Here is the code inside your button:

 - (void)click:(id)sender { int randomRow = <>;//Your random method int currentRow = [_picker selectedRowInComponent:0]; int i = 0; while(1) { i++; NSString *rowToSelectString = [NSString stringWithFormat:@"%d", currentRow]; NSDictionary *rowToSelectDictionary = @{@"row":rowToSelectString}; if(randomRow < currentRow) { // Go backward currentRow--; } else { // Go forward currentRow++; } [self performSelector:@selector(selectRowInPicker:) withObject:rowToSelectDictionary afterDelay:i*0.1];//Change the delay as you want if(currentRow == randomRow) { break; } } } 

And the trick:

 -(void)selectRowInPicker:(NSDictionary *)rowToSelectDictionary { NSInteger row = [[rowToSelectDictionary objectForKey:@"row"] integerValue]; [_picker selectRow:row inComponent:0 animated:YES]; } 

This work is good for me. Tell me if you have problems with this.

+5
source share

Finally, I applied a β€œfiner” version of the selected answer:

 - (IBAction)spinTheWheelButtonPressed:(UIButton *)sender { for (int i = 0; i < 30; i++) { NSUInteger randomRow = arc4random_uniform((int)[self.dataSource count]); [self performSelector:@selector(selectRowInPicker:) withObject:@(randomRow) afterDelay:i*0.1]; } } - (void)selectRowInPicker:(NSNumber *)randomRow { [self.picker selectRow:[randomRow integerValue] inComponent:0 animated:YES]; } 
+4
source share

I had a similar problem, but unfortunately there is no easy way to delay the animation as far as I know. So I went and placed the UIView on top of the pickerView. I subclassed this view to pass all its touches to the pickerView, and when something was selected, I just drew a layer with less opacity in the view, where the line usually stopped and animated it in the animation block, since the selection was always right in the middle of the pickerView.

+1
source share

All Articles