如何在按下按钮后以动画方式显示 UIPickerView?
我想在按下按钮后为 UIPickerView 制作动画。我已经将 UIPickerView 编码为隐藏在 viewDidLoad 上,并且在按下按钮后不会隐藏,但它的动画效果不像 ModalViewController 默认情况下的动画效果。我只希望我的 UIPickerView 能够像 ModalViewController 默认动画一样进行动画处理。
我已经在网站和网络上进行了研究,但我似乎无法正确完成。
这是我的代码:
#pragma mark - Picker View
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return 4;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
timersArray = [[NSMutableArray alloc] init];
[timersArray addObject:@"No timer"];
[timersArray addObject:@"15 seconds"];
[timersArray addObject:@"30 seconds"];
[timersArray addObject:@"60 seconds"];
return [timersArray objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if ([[timersArray objectAtIndex:row] isEqual:@"No timer"])
{
timerIndication.text = @"No timer selected";
timersPickerView.hidden = YES;
// Animation code to dismiss picker should go here
}
else if ([[timersArray objectAtIndex:row] isEqual:@"15 seconds"])
{
timerIndication.text = @"15 seconds selected";
timersPickerView.hidden = YES;
// Animation code to dismiss picker should go here
}
else if ([[timersArray objectAtIndex:row] isEqual:@"30 seconds"])
{
timerIndication.text = @"30 seconds selected";
timersPickerView.hidden = YES;
// Animation code to dismiss picker should go here
}
else if ([[timersArray objectAtIndex:row] isEqual:@"60 seconds"])
{
timerIndication.text = @"60 seconds selected";
timersPickerView.hidden = YES;
// Animation code to dismiss picker should go here
}
}
#pragma mark - Delay method
// This is where Send button should be enabled
- (IBAction)selectTimer
{
timersPickerView.hidden = NO;
// Animation code to present picker view should go here
}
I want to animate my UIPickerView after pressing the button. I already have coded my UIPickerView to be hidden on viewDidLoad and not hidden after pressing a button, but it doesn't animate like how a ModalViewController animates by default. I just want my UIPickerView to be animated just like a ModalViewController animates by default.
I've researched already on the site, and on the web, but I can't seem to do it properly.
Here's my code:
#pragma mark - Picker View
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return 4;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
timersArray = [[NSMutableArray alloc] init];
[timersArray addObject:@"No timer"];
[timersArray addObject:@"15 seconds"];
[timersArray addObject:@"30 seconds"];
[timersArray addObject:@"60 seconds"];
return [timersArray objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if ([[timersArray objectAtIndex:row] isEqual:@"No timer"])
{
timerIndication.text = @"No timer selected";
timersPickerView.hidden = YES;
// Animation code to dismiss picker should go here
}
else if ([[timersArray objectAtIndex:row] isEqual:@"15 seconds"])
{
timerIndication.text = @"15 seconds selected";
timersPickerView.hidden = YES;
// Animation code to dismiss picker should go here
}
else if ([[timersArray objectAtIndex:row] isEqual:@"30 seconds"])
{
timerIndication.text = @"30 seconds selected";
timersPickerView.hidden = YES;
// Animation code to dismiss picker should go here
}
else if ([[timersArray objectAtIndex:row] isEqual:@"60 seconds"])
{
timerIndication.text = @"60 seconds selected";
timersPickerView.hidden = YES;
// Animation code to dismiss picker should go here
}
}
#pragma mark - Delay method
// This is where Send button should be enabled
- (IBAction)selectTimer
{
timersPickerView.hidden = NO;
// Animation code to present picker view should go here
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以使用以下代码在按下按钮后对选取器视图进行动画处理:
不要忘记将以下代码添加到您的 viewDidLoad 方法中,
它的作用是使选取器视图在第一次单击时从屏幕顶部掉落到使选择器视图消失,您只需再次单击该按钮即可。从下一次单击开始,选择器视图就会出现并消失。希望它有帮助并有效:)
You can use the following code for animating the picker view after pressing the button:
Don't forget to add the following code to your viewDidLoad method
What it does is it makes the picker view fall from the top of the screen on 1st click and to make the picker view disappear,you can simply click the button again.From the next click the picker view just appears and vanishes..Hope it helps and works :)
您可以创建一个视图控制器,然后在控制器内添加 UIPickerView ,然后使用:
[selfpresentModalViewController:viewControllerWithPickerViewanimated:YES];
或者您可以添加未隐藏的 UIPickerView,但其 y 值大于屏幕,例如 480.0 或更大,然后使用 UIView 动画将 UIPickerView 从该位置移动到屏幕上可见的位置,这将模拟 ModalViewController 动画。
You can create a viewcontroller and then add the UIPickerView inside the controller and then use:
[self presentModalViewController:viewControllerWithPickerView animated:YES];
or you can add your UIPickerView not hidden but with a y value bigger than the screen, like 480.0 or bigger and then use UIView animations to move the UIPickerView from that position to a position visible on the screen, that would emulate the ModalViewController animation.
您可以使用以下函数来执行选择器视图的动画。
将
isPickerDisplay 定义为 BOOL 并将其初始化为 ViewDidLoad ,如下所示 -
这将管理 UIPickerView 的隐藏和显示功能。
You can use below function to perform animation of picker view.
}
Define isPickerDisplay as BOOL and initialize this in to ViewDidLoad as given below -
This will manage hide and show functionality for UIPickerView.
Swift 3 解决方案
我同意@EshwarChaitanya,alpha 属性是可动画的,这是一个适当的用途。
我不确定你想如何隐藏它,所以我将它留给你。
Swift 3 Solution
I agree with @EshwarChaitanya, the alpha property is animatable and this is an appropriate use.
I am not sure how you want to hide it so I will leave it up to you.
当您按下按钮时,执行操作 [self.view addSubVuew:yourPickerView]; 。难道是无业无成吗?
When you press the button, do the action [self.view addSubVuew:yourPickerView]; . Is it workless?
Swift 5:向上滑动动画
1 - 将高度设置为 216(PickerView 标准)。
2- 设置前导和尾随到安全区域。
3- 将底部设置为“SuperViewBottom”,即-216。
4- 将第 3 行中的 IBOutlet 作为 NSLayoutConstraint 可选。
然后:
它应该像苹果标准动画一样工作。
祝你好运
Swift 5: Slide up animation
1- Set hight to 216 (PickerView Standard).
2- Set leading an trailing to safe Area.
3- Set Bottom to "SuperViewBottom" as -216.
4- Make a IBOutlet from line 3 as NSLayoutConstraint optional.
and then:
it should working like Apple standard animation.
Good luck ????