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

问题描述:

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

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.

这是我的代码:

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


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

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

}

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

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