【问题标题】:Is there any way to tell if UIPickerView is spinning?有什么方法可以判断 UIPickerView 是否在旋转?
【发布时间】:2014-09-27 02:35:18
【问题描述】:

有什么方法可以判断 UIPickerView 是否在旋转? 我需要在过渡时禁用一些 UI 元素。

【问题讨论】:

标签: ios objective-c uipickerview


【解决方案1】:

对此没有委托方法,但是您可以检查animationKeys 计数,因为UIPickerViewUIView 的子类:

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

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

也许pickerView:titleForRow:forComponent: 是放置这段代码的好地方?

【讨论】:

  • 不起作用。我想我将在 didSelectRow 事件中保存行号并将其与 selectedRowInComponent 进行比较。如果它们不相等,则它正在旋转。
【解决方案2】:

我通过保存didSelectRow 中的行号并将其与selectedRowInComponent 中的行进行比较来解决它。

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

我还创建了一个布尔值,用于在微调器启动时调用方法。

-(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 在 Swift 4.2 中的解决方案

1.创建 2 个变量

var lastPickedRow = 0
var pickerInMotion: Bool = false

2。保存didSelectRow 中的行号并将其与selectedRowInComponent 中的行进行比较。

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

3。创建运动开始时调用的方法

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

4.在didSelectRow

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

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

5.在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
}

【讨论】:

    猜你喜欢
    • 2012-05-30
    • 2014-04-18
    • 1970-01-01
    • 2012-09-03
    • 1970-01-01
    • 1970-01-01
    • 2017-04-27
    • 2019-10-20
    相关资源
    最近更新 更多