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

时间:2011-10-28 03:05:13

标签: iphone xcode 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
}

6 个答案:

答案 0 :(得分:10)

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

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

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

答案 1 :(得分:5)

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

[self presentModalViewController:viewControllerWithPickerView animated:YES];

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

答案 2 :(得分:2)

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

-(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的隐藏和显示功能。

答案 3 :(得分:1)

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

我不确定你是怎么想隐藏的,所以我会把它留给你。

答案 4 :(得分:0)

按下按钮时,执行操作[self.view addSubVuew:yourPickerView]; 。它没用吗?

答案 5 :(得分:0)

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 }
   
    }


}

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

祝你好运?