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

Raz*_*Raz 8 animation uipickerview ios

我用a UIPickerView来显示随机数.用户可以按下按钮,从而触发随机选择内部的数字UIPickerView.

UIPickerView当我调用方法时,在里面显示多少个对象或数字并不重要:

[self.picker selectRow:randomRow inComponent:0 animated:YES];
Run Code Online (Sandbox Code Playgroud)

它始终以相同的时间间隔进行动画处理.

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

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

[UIView beginAnimations:@"identifier" context:nil];
// code
[UIView commitAnimations];
Run Code Online (Sandbox Code Playgroud)

但这似乎是一个死胡同.

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

// 0.34f is the approximate defualt apple animation
[UIView animateWithDuration:0.34f animations:^{

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

} completion:^(BOOL finished) {

[UIView animateWithDuration:0.34f animations:^{

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

} completion:nil];
}];
Run Code Online (Sandbox Code Playgroud)

任何帮助,将不胜感激.

Nho*_*yen 5

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

以下是按钮的内部代码:

- (void)click:(id)sender
{
    int randomRow = <>;//Your random method

    int currentRow = [_picker selectedRowInComponent:0];

    int i = 0;
    while(1)
    {
        i++;
        NSString *rowToSelectString = [NSString stringWithFormat:@"%d", currentRow];
        NSDictionary *rowToSelectDictionary = @{@"row":rowToSelectString};

        if(randomRow < currentRow)
        {
            // Go backward
            currentRow--;
        }
        else
        {
            // Go forward
            currentRow++;
        }


        [self performSelector:@selector(selectRowInPicker:) withObject:rowToSelectDictionary afterDelay:i*0.1];//Change the delay as you want

        if(currentRow == randomRow)
        {
            break;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

诀窍:

-(void)selectRowInPicker:(NSDictionary *)rowToSelectDictionary
{
    NSInteger row = [[rowToSelectDictionary objectForKey:@"row"] integerValue];
    [_picker selectRow:row inComponent:0 animated:YES];
}
Run Code Online (Sandbox Code Playgroud)

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


Raz*_*Raz 5

最后,我已经实现了所选答案的"更薄"版本:

- (IBAction)spinTheWheelButtonPressed:(UIButton *)sender
{
    for (int i = 0; i < 30; i++) {

        NSUInteger randomRow = arc4random_uniform((int)[self.dataSource count]);

        [self performSelector:@selector(selectRowInPicker:) withObject:@(randomRow) afterDelay:i*0.1];
    }
}

- (void)selectRowInPicker:(NSNumber *)randomRow
{
    [self.picker selectRow:[randomRow integerValue] inComponent:0 animated:YES];
}
Run Code Online (Sandbox Code Playgroud)