xcode 按下按钮后如何动画显示 UIPickerView?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7924704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:15:48  来源:igfitidea点击:

How can I animate displaying UIPickerView after pressing button?

iphonexcodeuipickerview

提问by jaytrixz

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.

我想在按下按钮后为我的 UIPickerView 设置动画。我已经将我的 UIPickerView 编码为隐藏在 viewDidLoad 上,而不是在按下按钮后隐藏,但它的动画效果不像 ModalViewController 默认的动画效果。我只是希望我的 UIPickerView 像默认情况下的 ModalViewController 一样动画。

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
}

回答by Eshwar Chaitanya

You can use the following code for animating the picker view after pressing the button:

按下按钮后,您可以使用以下代码为选择器视图设置动画:

-(IBAction)button:(id)sender
{

   [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.6];
    CGAffineTransform transfrom = CGAffineTransformMakeTranslation(0, 200);
    PickerView.transform = transfrom;
    PickerView.alpha = PickerView.alpha * (-1) + 1;
    [UIView commitAnimations];

}

Don't forget to add the following code to your viewDidLoad method

不要忘记将以下代码添加到您的 viewDidLoad 方法中

PickerView.alpha = 0;    
[self.view addSubview:PickerView];

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 :)

它的作用是使选择器视图在第一次单击时从屏幕顶部掉落并使选择器视图消失,您只需再次单击该按钮即可。从下一次单击开始,选择器视图就会出现并消失......希望它帮助和工作:)

回答by Paul N

You can create a viewcontroller and then add the UIPickerView inside the controller and then use:

您可以创建一个视图控制器,然后在控制器中添加 UIPickerView,然后使用:

[self presentModalViewController:viewControllerWithPickerView animated:YES];

[self presentModalViewController:viewControllerWithPickerView 动画: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.

或者您可以添加不隐藏但 y 值大于屏幕的 UIPickerView,例如 480.0 或更大,然后使用 UIView 动画将 UIPickerView 从该位置移动到屏幕上可见的位置,这将模拟 ModalViewController 动画。

回答by Vikas Sawant

You can use below function to perform animation of picker view.

您可以使用以下功能来执行选择器视图的动画。

-(void)hideShowPickerView {

if (!isPickerDisplay) {
    [UIView animateWithDuration:0.25 animations:^{
        CGRect temp = self.categoryPicker.frame;
        temp.origin.y = self.view.frame.size.height - self.categoryPicker.frame.size.height;
        self.categoryPicker.frame = temp;
    } completion:^(BOOL finished) {
        NSLog(@"picker displayed");
        isPickerDisplay = YES;
    }];
}else {
    [UIView animateWithDuration:0.25 animations:^{
        CGRect temp = self.categoryPicker.frame;
        temp.origin.y = self.view.frame.size.height;
        self.categoryPicker.frame = temp;
    } completion:^(BOOL finished) {
        NSLog(@"picker hide");
        isPickerDisplay = NO;
    }];
}

}

}

Define isPickerDisplay as BOOL and initialize this in to ViewDidLoad as given below -

将 isPickerDisplay 定义为 BOOL 并将其初始化为 ViewDidLoad,如下所示 -

isPickerDisplay = YES;
[self hideShowPickerView];

This will manage hide and show functionality for UIPickerView.

这将管理 UIPickerView 的隐藏和显示功能。

回答by Mark Moeykens

Swift 3 Solution

Swift 3 解决方案

I agree with @EshwarChaitanya, the alpha property is animatable and this is an appropriate use.

我同意@EshwarChaitanya,alpha 属性是可动画的,这是一个合适的用途。

override func viewDidLoad() {
    super.viewDidLoad()

    timersPickerView.alpha = 0
}

@IBAction func selectTimer() {
    UIView.animate(withDuration: 0.3, animations: {
        self.timersPickerView.alpha = 1
    })
}

I am not sure how you want to hide it so I will leave it up to you.

我不确定你想如何隐藏它,所以我会留给你。

回答by fayTu

When you press the button, do the action [self.view addSubVuew:yourPickerView]; . Is it workless?

当你按下按钮时,做动作 [self.view addSubVuew:yourPickerView]; . 是不是没用?