Initiate a UIPicker from a UIbutton
I would like to make a UIbutton, by press it, it trigger the appearance of a UIPicker from bottom with animation.
M开发者_开发问答ay I know how and what code can i initiate the UIpicker from button? And where should i place and set the UIpicker in Interface Builder? and lastly, after choose from the picker, how can it move away?
Many thanks!
You need to look up some documentation on how to make a UIPickerView. There are tons of tutorials out there, even video tutorials. Just search around. It's part of becoming a good programmer. I personally would set up my picker in interface builder, and set up an IBOutlet to it. I would then, in the .h file of your viewController, set up a method called -(IBAction)showPicker Them link the method to your button. In your .m file of your viewController you should have
-(IBAction)showPicker
{
[UIView beginAnimations];//tells the compiler to start a new animation
[UIView setAnimationDuration:1.0];//in seconds
thepicker.frame = CGRectMake(100, 100, 200, 200);//move the picker to a specific position
[UIView commitAnimations];//tells the compiler to start the animations
}
the beginAnimations and commitAnimations is the code that says any code between us will be animated. So you can animate their alpha (transparency), their positions, and others as well. thepicker refers to your IBOutlet name, whatever you called your picker. I would move the picker out of the screen in your interface builder, then when you press the button, move it into postion with
thepicker.frame = CGRectMake(x,y,width,height);
then, to move it away, just use the same code but change the frame to be out of the screen when you animate it.
BTW, I would create your pickerView first in the viewdidload method, instead of creating it on tapping the button. Otherwise, you will constantly be creating UIPickerViews everytime the button is hit.
精彩评论