gpt4 book ai didi

ios - 延长 UIPickerView [selectRow : inComponent: animated:]'s animation

转载 作者:可可西里 更新时间:2023-11-01 03:08:12 25 4
gpt4 key购买 nike

我正在使用 UIPickerView 来显示随机数。用户可以按下一个按钮,从而触发在 UIPickerView 中随机选择一个数字。

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

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

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

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

我试过将它放在动画 block 中:

[UIView beginAnimations:@"identifier" context:nil];
// code
[UIView commitAnimations];

但这似乎是一条死胡同。

我也试过在完成 block 中执行它:

// 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];
}];

如有任何帮助,我们将不胜感激。

最佳答案

我做了一个项目并玩了一段时间,直到找到这个棘手的解决方案。它基于方法 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;
}
}
}

技巧:

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

这对我来说效果很好。如果您遇到问题,请告诉我。

关于ios - 延长 UIPickerView [selectRow : inComponent: animated:]'s animation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23739040/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com