0

Is there any way to tell if UIPickerView is spinning? I need to disable some UI elements when it is in transition.

JVC
  • 540
  • 3
  • 17

3 Answers3

1

There are no delegate method for this however you can check the animationKeys count since a UIPickerView is a subclass of UIView:

BOOL isSpinning = myPickerView.layer.animationKeys.count > 0;

if(isSpinning){
   NSLog(@"disable");
}else{
   NSLog(@"enable");
}

Maybe pickerView:titleForRow:forComponent: is maybe a good place to put this code ?

meda
  • 45,103
  • 14
  • 92
  • 122
  • Doesn't work. I think I'm going to save the row number in the didSelectRow event and compare it to selectedRowInComponent. If they are not equal, it is spinning. – JVC Sep 27 '14 at 05:04
1

I solved it by saving the row number in didSelectRow and comparing it to the row in selectedRowInComponent.

-(BOOL) isCardPickerSpinning{
return (lastCardPickerRow != [cardPicker selectedRowInComponent:0]);}

I also created a boolean that will be used to call a method when the spinner is put in motion.

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
  inComponent:(NSInteger)component
{
 lastCardPickerRow = row;
 pickerInMotion = NO;
 //update UI code goes here
 eventSwitch.enabled = YES;
}

 -(void)pickerViewMotionStart
{
    //disable my UI
    eventSwitch.enabled = NO;
}

- (UIView *)pickerView:(UIPickerView *)pickerView
        viewForRow:(NSInteger)row
      forComponent:(NSInteger)component
       reusingView:(UIView *)view {

UILabel *pickerLabel = (UILabel *)view;
 if (pickerLabel == nil) {
    CGRect frame = CGRectMake(0.0, 0.0, 200, 32);
    pickerLabel = [[UILabel alloc] initWithFrame:frame];
    pickerLabel.textAlignment=NSTextAlignmentLeft;
 }
 if (!pickerInMotion)
 {
    pickerInMotion = YES;
    [self pickerViewMotionStart];
 }
 pickerLabel.text = @"SomeString";
 return pickerLabel;

}
JVC
  • 540
  • 3
  • 17
0

JVC's solution in Swift 4.2

1. Create 2 variables

var lastPickedRow = 0
var pickerInMotion: Bool = false

2. Saving the row number in didSelectRow and comparing it to the row in selectedRowInComponent.

var isPickerSpinning: Bool {
    return lastPickedRow != pickerView.selectedRow(inComponent: 0)
}

3. Create a method to call, when motion started

func pickerViewMotionStart(){
    //Do something when motion started
    button.alpha = 0
}

4. In didSelectRow

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

    lastPickedRow = row
    pickerInMotion = false
    //Do something when motion ended
    button.alpha = 1
}

5. In viewForRow

func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
    //create a label to write something
    let label = UILabel()
    label.frame = CGRect(x: 0, y: 0, width: pickerView.frame.width, height: 40)
    label.font = UIFont.systemFont(ofSize: 22)
    label.textAlignment = .center

    //Check if picker is moving or not        
    if !pickerInMotion {
        pickerInMotion = true
        self.pickerViewMotionStart()
    }
    label.text = "something"
    return label
}
Dris
  • 881
  • 8
  • 19