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

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

最佳答案

您可以使用以下代码在按下按钮后为选取器 View 设置动画:

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

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

关于iphone - 如何在按下按钮后以动画方式显示 UIPickerView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7924704/

相关文章:

iphone - 使用 OS X 和 Vmware 进行 iPhone 开发是否成功?

iphone - UIImage 元数据

ios - 如何使用 Xcode 6 (Swift) 添加引脚(注释)

ios - 如何获取组件在 UIPickerView 中的位置?

ios - Xamarin ios 隐藏\删除 UIPickerView

ios - swift 同一 UIView 中的多个自定义 UIPickerView

iphone - iphone os 框架本身会抛出 NSException 吗?

ios - iOS 设备上的葫芦测试启动应用程序但卡住在 "Scenario"

iphone - 将 UIView 添加到 ScrollView 时发生奇怪的事情

iphone - 切换启动 View Controller Xcode