简体   繁体   中英

UIView animation

替代文字

In the figure, what you see in "red" is another view that slides up once you click the first + button. This "red" view does not have a separate view controller. On every "+" button click, I want this view to slide up with a different element embedded in it. For instance, on the first "+" button click , I would like this red view to slide up with a datepicker embedded in it. On second "+" button click, I would like this red view to slide up with a UIPicker View embedded in it. I have the elements: UIDatepicker and UIPicker prepared. I don't know when and where to embed them and what should be their frame size.

I am initializing the "red" view in the viewDidLoad method

viewDidLoad {

CGRect frame=CGRectMake(0, CGRectGetMaxY(self.view.bounds)-70, 320, 150);

popup=[[UIView alloc]init];

popup.backgroundColor=[UIColor redColor];

[popup setFrame:frame];

}

and I am sliding it up in

-(void) btnClick:(id)sender { frame=CGRectMake(0, 240, 320, 150);

[UIView beginAnimations:nil context:nil];

    [popup setFrame:frame];

[UIView commitAnimations];  

}

You should not modify the popup hierarchy everytime you click on a + button. Create as many popup as you need and show the appropriate popup when you need it, it will allows you to easely swicth from one popup to another at the same time :

[UIView beginAnimations:nil context:nil];
currentlyOpenPopup.frame = CGRectMake(0.0, self.bounds.size.height, 320,150);
currentlyOpenPopup = DatePickerPopup //here comes the if blablabl set the right popup to show;
currentlyOpenPopup.frame = CGRectMake(0.0, self.bounds.size.height-150, 320, 150);
[UIView commitAnimations]; 

Create your popups in ViewDidLoad for example:

datePickerPopup = [[UIView alloc] initWithFrame:CGRectFrame(0,0,320,150)];
UIDatePicker* dp = [[UIDatePicker alloc] initWithFrame:CGRectMake(0,0,320,216)];
[datePickerPopup addSubView:dp];
[dp release];

the UIView is there only if you need to place multiple object on one popup.

Add all the code for the datepicker and regular picker as you normally would.

When a button is pressed add the picker/datepicker to popup.

picker = [[UIPickerView alloc] init];
picker.frame = CGRectMake(0,0,picker.frame.size.width,picker.frame.size.height);
[popup addSubview:picker];

//your code:
[UIView beginAnimations:nil context:nil];
[popup setFrame:frame];
[UIView commitAnimations];

when hiding the popup view add:

  [UIView setAnimationDelegate:self];
  [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context::)];

Add a method:

- (void)animationDidStop:(NSString*)animationID finished:(BOOL)finished context:(void *)context 

Remove the pickerView or whatever you added in that method..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM