Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 当selectRow动画完成时,如何从UIPickerView获取回调?_Iphone_Uipickerview - Fatal编程技术网

Iphone 当selectRow动画完成时,如何从UIPickerView获取回调?

Iphone 当selectRow动画完成时,如何从UIPickerView获取回调?,iphone,uipickerview,Iphone,Uipickerview,我有一个UIPickerView,我希望在selectRow动画完成时收到通知 我在我的视图控制器中尝试了以下方法,该方法引用了UIPickerView,但无效: -(void)viewDidLoad { ... [UIPickerView setAnimationDelegate:self]; [UIPickerView setAnimationDidStopSelector:@selector(animationFin ished:finished:context];

我有一个UIPickerView,我希望在selectRow动画完成时收到通知

我在我的视图控制器中尝试了以下方法,该方法引用了UIPickerView,但无效:

-(void)viewDidLoad
{
    ...
    [UIPickerView setAnimationDelegate:self];
    [UIPickerView setAnimationDidStopSelector:@selector(animationFin ished:finished:context];
    ...
}

- (void)animationFinishedNSString *)animationID finishedBOOL)finished contextvoid *)context
{
    if (finished) {

    }

}
然后在我的代码中的某个地方,我启动动画:

[picker selectRow:random() % pickerDataCount inComponent:0 animated:YES];

您需要将方法调用嵌套到beginAnimations/commitAnimation块中

- (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...
}

您可以从viewForRow向self发布通知 当它要求查看您感兴趣的组件和行时

您只需要将行和组件作为属性保存 并在调用selectRow之前进行设置。在viewForRow中

如果((组件==[自身组件]&&&(行==[自身行])
向self发布一个通知

我用这里提到的不同答案混合解决了这个问题。其行为是,它将等待滚动完成,然后保存所选的值

  • 创建两个变量,用于存储滚动状态和应保存状态。在didSet中,您将检查选择器滚动时是否按下了保存按钮。如果是,请在选择器完成滚动后调用保存

    var shouldSave = false
    var pickerViewIsScrolling = false {
        didSet {
            if !pickerViewIsScrolling && shouldSave {
                save()
            }
        }
    }
    
  • 要识别选择器是否正在滚动,请在选择器的
    viewForRow
    方法中添加
    pickerviewscrolling=true

    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
        pickerViewIsScrolling = true
        ...
    }
    
  • 要识别选择器是否已停止滚动,请将
    pickerviewscrolling=false
    添加到选择器的
    didSelectRow

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
         pickerViewIsScrolling = false 
         ...
    }
    
  • save()
    函数中添加以下内容,以检查选择器是否正在滚动(并在停止后保存),还是没有滚动并直接保存

    func save() {
         if(pickerViewIsScrolling){
              shouldSave = true
              return
         }
    
         // Save the Data...
    
    }
    
  • 最后,将这一行添加到您的
    viewdideappear
    函数中,以捕获初始生成视图的
    pickerwiscrolling=true

    override func viewDidAppear(_ animated: Bool) {
    
         super.viewDidAppear(animated)
    
         pickerViewIsScrolling = false
    
         ...
    
    }
    
  • 这对我来说很好。我还实现了在按下save并等待滚动结束时停用按钮。因此,用户不会感到困惑,为什么在滚动停止之前什么都没有发生