I have a UIPickerView and I would like to be notified when the selectRow animation is done.
I tried the following approach in my view controller which has a reference to the UIPickerView and it won't work:
-(void)viewDidLoad
{
...
[UIPickerView setAnimationDelegate:self];
[UIPickerView setAnimationDidStopSelector:@selector(animationFin ished:finished:context];
...
}
- (void)animationFinishedNSString *)animationID finishedBOOL)finished contextvoid *)context
{
if (finished) {
}
}
Then somewhere in my code, I initiate the animation:
[picker selectRow:random() % pickerDataCount inComponent:0 animated:YES];
you need to nest the method call into a beginAnimations/commitAnimation block.
- (void) animationFinished:(NSString *)animationID finished:(BOOL)finished context:(void *)context {
NSLog(@"Here I am");
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
[UIView beginAnimations:@"1" context:nil]; // nil = dummy
[UIPickerView setAnimationDelegate:self];
[UIPickerView setAnimationDidStopSelector:@selector(animationFinished:finished:context:)];
[myPickerView selectRow:0 inComponent:0 animated:YES]; // jump with any swipe in picker always to row=0 as dummy to initiate animation
[UIView commitAnimations];
//...whatever comes in addition...
}
you could post a notification to self from viewForRow when it asks view for component & row you are interested.
You just need to hold row & component as properties and set them before you call selectRow. And, in viewForRow
if ( (component == [self component] && (row == [self row] ) post a notification to self
I solved it with a mix out of different Answers mentioned here. The behaviour will be that it will wait until the scrolling finished and then save the selected value.
Create two variables, which store the scrolling state and the should save state. In the didSet you will check, if the the save button has been pressed while the picker is scrolling. If yes, call save after the picker has finished scrolling.
var shouldSave = false var pickerViewIsScrolling = false { didSet { if !pickerViewIsScrolling && shouldSave { save() } } }
To recognize if the picker is scrolling, add
pickerViewIsScrolling = true
in theviewForRow
method of the picker.func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView { pickerViewIsScrolling = true ... }
To recognize if the picker has stopped scrolling add
pickerViewIsScrolling = false
to thedidSelectRow
of the picker.func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { pickerViewIsScrolling = false ... }
In your
save()
function add the following to check wether the picker is scrolling (and save after it stopped) or is not scrolling and save directly.func save() { if(pickerViewIsScrolling){ shouldSave = true return } // Save the Data... }
And finally add to your
viewDidAppear
function this line to catch thepickerViewIsScrolling = true
of the initial generated views.override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) pickerViewIsScrolling = false ... }
This works fine for me. I also implemented the deactivation of the button while save was pressed and it is waiting for the scrolling to finish. So the user won't be confused why nothing is happening until the scrolling stops.
来源:https://stackoverflow.com/questions/703293/how-to-get-callback-from-uipickerview-when-the-selectrow-animation-is-done