Is there any way to tell if UIPickerView is spinning?

我的未来我决定 提交于 2019-12-08 09:48:51

问题


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


回答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 ?




回答2:


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;

}



回答3:


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
}


来源:https://stackoverflow.com/questions/26070723/is-there-any-way-to-tell-if-uipickerview-is-spinning

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