如何在按下按钮后以动画方式显示 UIPickerView?

发布于 2024-12-12 08:10:09 字数 2120 浏览 0 评论 0原文

我想在按下按钮后为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

蓝戈者 2024-12-19 08:10:09

您可以使用以下代码在按下按钮后对选取器视图进行动画处理:

-(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];

}

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

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

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

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

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

真心难拥有 2024-12-19 08:10:09

您可以创建一个视图控制器,然后在控制器内添加 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.

说不完的你爱 2024-12-19 08:10:09

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

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

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

isPickerDisplay = YES;
[self hideShowPickerView];

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

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 = YES;
[self hideShowPickerView];

This will manage hide and show functionality for UIPickerView.

ぃ弥猫深巷。 2024-12-19 08:10:09

Swift 3 解决方案

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

override func viewDidLoad() {
    super.viewDidLoad()
    
    timersPickerView.alpha = 0
}

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

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

Swift 3 Solution

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

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.

楠木可依 2024-12-19 08:10:09

当您按下按钮时,执行操作 [self.view addSubVuew:yourPickerView]; 。难道是无业无成吗?

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

梦旅人picnic 2024-12-19 08:10:09

Swift 5:向上滑动动画

1 - 将高度设置为 216(PickerView 标准)。

2- 设置前导和尾随到安全区域。

3- 将底部设置为“SuperViewBottom”,即-216

4- 将第 3 行中的 IBOutlet 作为 NSLayoutConstraint 可选。

然后:

import UIKit

class ViewController: UIViewController {

 override func viewDidLoad() {

 super.viewDidLoad()

 bottom.constant = -216

}

@IBOutlet weak var bottom: NSLayoutConstraint!

-(IBAction)button:(id)sender
{
   UIView.animate(withDuration: 0.2, delay: 0.0, options: .curveEaseIn, animations:
        {
            self.bottom.constant = 0
        self.view.layoutIfNeeded()
        }) { (AnimationComplete ) in }
}

 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
       

        UIView.animate(withDuration: 0.2, delay: 0.0, options: .curveEaseIn, animations:
        {
            self.bottom.constant = -216
        self.view.layoutIfNeeded()
        }) { (AnimationComplete ) in }
   
    }


}

它应该像苹果标准动画一样工作。

祝你好运

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:

import UIKit

class ViewController: UIViewController {

 override func viewDidLoad() {

 super.viewDidLoad()

 bottom.constant = -216

}

@IBOutlet weak var bottom: NSLayoutConstraint!

-(IBAction)button:(id)sender
{
   UIView.animate(withDuration: 0.2, delay: 0.0, options: .curveEaseIn, animations:
        {
            self.bottom.constant = 0
        self.view.layoutIfNeeded()
        }) { (AnimationComplete ) in }
}

 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
       

        UIView.animate(withDuration: 0.2, delay: 0.0, options: .curveEaseIn, animations:
        {
            self.bottom.constant = -216
        self.view.layoutIfNeeded()
        }) { (AnimationComplete ) in }
   
    }


}

it should working like Apple standard animation.

Good luck ????

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文