Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iphone: expand and collapse a UIView programmatically

Tags:

xcode

iphone

ipad

I am very new to iPhone/iPad development. can you please help me create this programmatically. I want to expand/collapse a UIView programmatically.

This expandable/collapsable view will have some text field and lables which should appear and disappear with that view

like image 729
Ankit Sachan Avatar asked Dec 04 '22 23:12

Ankit Sachan


2 Answers

Suppose you have a UIView instance in the UIViewController class like this:

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(x, y, w1, h1)];
[self.view addSubview:view];

based on the requirement you set the view visibility as I did here .. I am not displaying the view when the controller is loading it's view ..

[view setHidden:YES];

Maintain a flag to check the visibility of the view instance .. lets say isViewVisible is my flag to check the visibility of the view .. I set it to NO in the begning ..

isHelpViewVisible = NO;

and I wrote an action method (viewClicked) here to expand and to collapse the view object , give this action method to a button instance and it will work.

- (void)viewClicked:(id)sender {

    if (!isViewVisible) {
        isViewVisible = YES;
        [view setHidden:NO];
        [UIView beginAnimations:@"animationOff" context:NULL]; 
        [UIView setAnimationDuration:1.3f];
        [view setFrame:CGRectMake(x, y, w1, h1)];
        [UIView commitAnimations];
    } else {
        isViewVisible = NO;
        [view setHidden:NO];
        [UIView beginAnimations:@"animationOff" context:NULL]; 
        [UIView setAnimationDuration:1.3f];
        [view setFrame:CGRectMake(x, y, width, hight)];
        [UIView commitAnimations];
    }

}

and add the textfields and labels objects to the view object as subviews and set the animation to those objects as well .. it will work.

like image 129
Dee Avatar answered Dec 14 '22 08:12

Dee


Replaces the following from above:

[UIView beginAnimations:@"animationOff" context:NULL];
[UIView setAnimationDuration:1.3f];
[view setFrame:CGRectMake(x, y, w1, h1)];
[UIView commitAnimations];

With the New way of doing animations:

[UIView animateWithDuration:1.3f animations:^{
    self.frame = CGRectMake( x1, x2, w1, h1);
} completion:^(BOOL finished) {
    // this gives us a nice callback when it finishes the animation :)
}];
like image 23
Danoli3 Avatar answered Dec 14 '22 08:12

Danoli3