Custom pop-up of UIPickerView or UIDatePicker (animation effect) for iOS Learning)

Source: Internet
Author: User
The UIPickerView used in the simple use of the UIPickerView control learned by iOS is displayed through textField. inputView = selectPicker; textField. inputAccessoryView = donw.lbar; this method is used in this process. If the UIPickerView or UIDatePicker control is activated by another button or event, how can it pop up like the system? To achieve this, we need to use the animation effect. 1. Create a Single View App project and add the following controls to the. xib file: two buttons and one UIDatePicker. 2. Create a connection between the xib and ViewController. Press and hold the Control key to create a ing between the three controls. The viewController. h code after creation is as follows:
#import <UIKit/UIKit.h>@interface ViewController : UIViewController@property (retain, nonatomic) IBOutlet UIDatePicker *pickerView;- (IBAction)popView:(id)sender;- (IBAction)inView:(id)sender;@property  (nonatomic, retain) NSString *string;@end
3. Hide pickerView

- (void)viewDidLoad{    [super viewDidLoad];    self.pickerView.frame = CGRectMake(0, 480, 320, 260);}

Put the pickerView under the screen.

4. Pop-up and pop-back pickerView

Set the animation when the pickerView pops up or goes back.
-(IBAction) popView :( id) sender {CGContextRef context = sums (); [UIView beginAnimations: nil context: context]; [UIView setAnimationCurve: sums]; [UIView setAnimationDuration: 0.6]; // animation duration, in seconds, floating point [self. view exchangeSubviewAtIndex: 0 withSubviewAtIndex: 1]; self. pickerView. frame = CGRectMake (0,245,320,260); [UIView setAnimationDelegate: self]; // call after the animation is complete AnimationFinished [UIView progress: @ selector (animationFinished)]; [UIView commitAnimations];}-(IBAction) inView :( id) sender {CGContextRef context = upload (); [UIView beginAnimations: nil context: context]; [UIView setAnimationCurve: UIViewAnimationCurveEaseInOut]; [UIView setAnimationDuration: 0.6]; // animation duration, in seconds, floating point self. pickerView. frame = CGRectMake (0,480,320, 260); [UIView setAnimationDelegate: self]; // call animationFinished [UIView finished: @ selector (animationFinished)]; [UIView commitAnimations];}-(void) animationFinished {NSLog (@ "animation ends! ");}

Calls back the function after the animation ends.

Run. The first image is half displayed, and the second image is all displayed. 4. The code block method is used as an animation. The pickerView is displayed and a separate method is written.
- (void)ViewAnimation:(UIView*)view willHidden:(BOOL)hidden {        [UIView animateWithDuration:0.3 animations:^{        if (hidden) {            view.frame = CGRectMake(0, 480, 320, 260);        } else {            [view setHidden:hidden];            view.frame = CGRectMake(0, 245, 320, 260);        }    } completion:^(BOOL finished) {        [view setHidden:hidden];    }];}
5. Call in Action
- (IBAction)popView:(id)sender {    [self ViewAnimation:self.pickerView willHidden:NO];}- (IBAction)inView:(id)sender {    [self ViewAnimation:self.pickerView willHidden:YES];}

This method is simpler and more practical.

PS: the above method can be used in TableViewCell click cell bounce back pickerView and other needs. Example source code: http://download.csdn.net/detail/totogo2010/4472062

Copyright Disclaimer: This article will be published at http://blog.csdn.net/totogo2010/, and you will be welcomed to enjoy the transfer and sharing. Please respect the work of the author. Keep this note and the author's blog link when reprinting. Thank you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.