How can I animate displaying UIPickerView after pressing button?

纵然是瞬间 提交于 2019-11-30 04:02:54

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

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.

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.

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!