How can I animate displaying UIPickerView after pressing button?

后端 未结 6 1324
日久生厌
日久生厌 2021-02-03 13:38

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 does

相关标签:
6条回答
  • 2021-02-03 13:41

    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.

    0 讨论(0)
  • 2021-02-03 13:41

    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.

    0 讨论(0)
  • 2021-02-03 13:43

    Swift 5: Slide up animation

    1- Set hight to 216 (PickerView Standard).

    2- Set leading an trailing to safe Area.

    3- Set Bottom to "SuperViewBottom" as -216.

    4- Make a IBOutlet from line 3 as NSLayoutConstraint optional.

    and then:

    import UIKit
    
    class ViewController: UIViewController {
    
     override func viewDidLoad() {
    
     super.viewDidLoad()
    
     bottom.constant = -216
    
    }
    
    @IBOutlet weak var bottom: NSLayoutConstraint!
    
    -(IBAction)button:(id)sender
    {
       UIView.animate(withDuration: 0.2, delay: 0.0, options: .curveEaseIn, animations:
            {
                self.bottom.constant = 0
            self.view.layoutIfNeeded()
            }) { (AnimationComplete ) in }
    }
    
     override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
           
    
            UIView.animate(withDuration: 0.2, delay: 0.0, options: .curveEaseIn, animations:
            {
                self.bottom.constant = -216
            self.view.layoutIfNeeded()
            }) { (AnimationComplete ) in }
       
        }
    
    
    }
    
    

    it should working like Apple standard animation.

    Good luck

    0 讨论(0)
  • 2021-02-03 13:44

    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.

    0 讨论(0)
  • 2021-02-03 13:46

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

    0 讨论(0)
  • 2021-02-03 14:06

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

    0 讨论(0)
提交回复
热议问题