How to get callback from UIPickerView when the selectRow animation is done?

时光怂恿深爱的人放手 提交于 2019-12-06 13:44:32

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.

  1. 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()
            }
        }
    }
    
  2. To recognize if the picker is scrolling, add pickerViewIsScrolling = true in the viewForRow method of the picker.

    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
        pickerViewIsScrolling = true
        ...
    }
    
  3. To recognize if the picker has stopped scrolling add pickerViewIsScrolling = false to the didSelectRow of the picker.

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
         pickerViewIsScrolling = false 
         ...
    }
    
  4. 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...
    
    }
    
  5. And finally add to your viewDidAppear function this line to catch the pickerViewIsScrolling = 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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!