0

我在尝试实现 UIPickerView 在按下按钮时向上滑动的效果时遇到问题。

   -(IBAction)slideUp:(id)sender{
if([sender currentTitle] == @"Select"){

[pickerView setFrame: CGRectMake(0, 415.0, 320.0, 216.0)];
[UIView beginAnimations: nil context: NULL];
[UIView setAnimationDuration: 0.5];
[UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
[pickerView setFrame: CGRectMake(0, 201.0, 320.0, 216.0)];
[UIView commitAnimations];
[slideButton setTitle:@"Done" forState:UIControlStateNormal];

} }

如您所见,代码首先检查按钮的标题是“选择”还是“完成”,如果是选择,它会在 UIPickerView 中滑动。但是,我每次构建应用程序时,第一次按下按钮时,UIPickerView 立即出现在结束位置,然后滑出视图。按钮标题不会改变。在这个初始故障之后,它工作正常。

如果您知道为什么会发生这种情况,或者可以想到一个快速解决方案,我将非常感激。

卢克

4

1 回答 1

0

我不确定您为什么会遇到此故障,但是我会在您的代码中做一些不同的事情。首先,我会用其他方法(可能在你创建它之后)在远处的某个地方设置选择器视图的初始框架。然后我只需将动画块之前的初始帧引用到一个新的 CGRect 结构,我将对其进行动画处理并重新分配。其次,如果您只想为选取器视图的 y 组件设置动画,那么我也会在动画中指定它。

这是我对您的代码所做的更改:

-(IBAction)slideUp:(id)sender {

if([sender currentTitle] == @"Select") {

CGRect pickerFrame = pickerView.frame;   // You set pickerView.frame in some other method.

[UIView beginAnimations: nil context: NULL];
[UIView setAnimationDuration: 0.5];
[UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];

pickerFrame.origin.y = 201.0f;
pickerView.frame = pickerFrame;

[UIView commitAnimations];

[slideButton setTitle:@"Done" forState:UIControlStateNormal];

}

告诉我这是否有任何好处。

于 2011-08-07T01:45:23.860 回答