How can I animate the display of a UIPickerView after clicking a button

I want to revive my UIPickerView after clicking a button. I already encoded my UIPickerView to hide in viewDidLoad, and not hidden after clicking a button, but it doesnโ€™t enliven, as the default animation of ModalViewController. I just want my UIPickerView to be animated, like the default ModalViewController animation.

I have researched already on the site and on the Internet, but I can not do it right. Can anybody help me?

Thanks a lot!:)

Here is 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 } 
+6
source share
5 answers

You can use the following code to animate the selection view after clicking 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]; } 

Remember to add the following code to your viewDidLoad method

 PickerView.alpha = 0; [self.view addSubview:PickerView]; 

What he does is that the pickup selection falls from the top of the screen on the 1st click and to disappear the selection view, you can just simply click on the button. From the next click, the selection view appears and disappears .. We hope this helps and works :)

+10
source

You can create a view manager and then add a UIPickerView inside the controller and then use:

[self presentModalViewController: viewControllerWithPickerView animated: YES];

or you can add your UIPickerView not hidden, but with a larger value than the screen, for example 480.0 or more, and then use the UIView animation to move the UIPickerView from this position to the position visible on the screen, which will emulate the ModalViewController animation.

+5
source

You can use the function below to animate the presentation of the selection.

 -(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 it in ViewDidLoad as follows -

 isPickerDisplay = YES; [self hideShowPickerView]; 

This will hide and show functions for UIPickerView.

+2
source

Swift 3 Solution

I agree with @EshwarChaitanya, the alpha property is animated, and this is a suitable use.

 override func viewDidLoad() { super.viewDidLoad() timersPickerView.alpha = 0 } @IBAction func selectTimer() { UIView.animate(withDuration: 0.3, animations: { self.timersPickerView.alpha = 1 }) } 

I'm not sure how you want to hide it, so that I leave it to you.

+1
source

When you click the button, perform the action [self.view addSubVuew: yourPickerView] ;, is it carefree?

0
source

Source: https://habr.com/ru/post/900258/


All Articles