Iphone 当selectRow动画完成时,如何从UIPickerView获取回调?
我有一个UIPickerView,我希望在selectRow动画完成时收到通知 我在我的视图控制器中尝试了以下方法,该方法引用了UIPickerView,但无效:Iphone 当selectRow动画完成时,如何从UIPickerView获取回调?,iphone,uipickerview,Iphone,Uipickerview,我有一个UIPickerView,我希望在selectRow动画完成时收到通知 我在我的视图控制器中尝试了以下方法,该方法引用了UIPickerView,但无效: -(void)viewDidLoad { ... [UIPickerView setAnimationDelegate:self]; [UIPickerView setAnimationDidStopSelector:@selector(animationFin ished:finished:context];
-(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发布一个通知我用这里提到的不同答案混合解决了这个问题。其行为是,它将等待滚动完成,然后保存所选的值
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
...
}