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.
Here's my code:
#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];
}
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 :)