ios – 延长UIPickerView [selectRow:inComponent:animated:]的动画

前端之家收集整理的这篇文章主要介绍了ios – 延长UIPickerView [selectRow:inComponent:animated:]的动画前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用UIPickerView来显示随机数.用户可以按下按钮,从而触发随机选择UIPickerView内的数字.

当我调用方法时,UIPickerView中显示的对象或数字有多少并不重要:

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

它总是以相同的时间间隔进行动画处理.

是否有任何选项或方法可以延长上述方法的动画时间间隔?

我试过把它放在一个动画块中:

  1. [UIView beginAnimations:@"identifier" context:nil];
  2. // code
  3. [UIView commitAnimations];

但这似乎是一个死路一条.

我也尝试在完成块中执行它:

  1. // 0.34f is the approximate defualt apple animation
  2. [UIView animateWithDuration:0.34f animations:^{
  3.  
  4. [self.picker selectRow:randomRow inComponent:0 animated:YES];
  5.  
  6. } completion:^(BOOL finished) {
  7.  
  8. [UIView animateWithDuration:0.34f animations:^{
  9.  
  10. [self.picker selectRow:randomRow inComponent:0 animated:YES];
  11.  
  12. } completion:nil];
  13. }];

任何帮助,将不胜感激.

解决方法

我做了一个项目并玩了一段时间,直到找到这个棘手的解决方案.它基于performSelector方法:afterDelay

以下是按钮的内部代码

  1. - (void)click:(id)sender
  2. {
  3. int randomRow = <>;//Your random method
  4.  
  5. int currentRow = [_picker selectedRowInComponent:0];
  6.  
  7. int i = 0;
  8. while(1)
  9. {
  10. i++;
  11. NSString *rowToSelectString = [NSString stringWithFormat:@"%d",currentRow];
  12. NSDictionary *rowToSelectDictionary = @{@"row":rowToSelectString};
  13.  
  14. if(randomRow < currentRow)
  15. {
  16. // Go backward
  17. currentRow--;
  18. }
  19. else
  20. {
  21. // Go forward
  22. currentRow++;
  23. }
  24.  
  25.  
  26. [self performSelector:@selector(selectRowInPicker:) withObject:rowToSelectDictionary afterDelay:i*0.1];//Change the delay as you want
  27.  
  28. if(currentRow == randomRow)
  29. {
  30. break;
  31. }
  32. }
  33. }

诀窍:

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

这对我很有用.告诉我你是否遇到问题.

猜你在找的iOS相关文章