How can I animate displaying UIPickerView after pressing button?
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.