How to create a folding UIView

I need to create an extensible and collapsible UIView, and I cannot pay for a third-party control.

This is basically how I would like it to behave:

At the top there should be a UIButton (or similar) that allows the user to switch between expanded and collapsed.

When expanding, I want to be able to host another UIView (for example, a calendar), and when minimizing, the surrounding controls should move up.

Does anyone have any ideas how to implement this simply - noob here :(

+3
source share
2 answers

a ViewController , , , "" "", : UIButtons :

[button addTarget:self action:@selector(eventMethod:)
     forControlEvents:UIControlEventTouchUpInside];

:

#define COLAPSED_HEIGHT 30

-(void)expand
    {   
        CGRect s= [self getScrerenBoundsForCurrentOriantation];
        CGRect f = self.view.frame;
        f.origin.y =s.size.height-self.view.frame.size.height;  
        [UIView beginAnimations:@"expand" context:NULL];
        [UIView setAnimationDelegate:self];
        [UIView setAnimationDidStopSelector:@selector(animationFinished:finished:context:)];    
        self.view.frame=f;

           //CHANGE YOUR EXPAND/COLLAPSE BUTTON HERE

        [UIView commitAnimations];
        self.thumbScrollerIsExpanded=YES;
    }

    -(void)collapseView
    {
        //re-factored so that this method can be called in ViewdidLoad
        CGRect s= [self getScrerenBoundsForCurrentOriantation];
        CGRect f = self.view.frame;

        f.origin.y =(s.size.height-COLAPSED_HEIGHT);

        self.view.frame = f;

        self.thumbScrollerIsExpanded=NO;    //thumbScrollerIsExpanded is a BOOL property
    }


    - (void)collapse
    {       
        [UIView beginAnimations:@"collapse" context:NULL];
        [UIView setAnimationDelegate:self];
        [UIView setAnimationDidStopSelector:@selector(animationFinished:finished:context:)];    
        [self collapseView];
           //CHANGE YOUR EXPAND/COLLAPSE BUTTON HERE    
        [UIView commitAnimations];

    }


-(CGRect)getScrerenBoundsForCurrentOriantation
{
    return [self getScrerenBoundsForOriantation:[[UIDevice currentDevice] orientation]];
}


-(CGRect)getScrerenBoundsForOriantation:(UIInterfaceOrientation)_orientation
{
    UIScreen *screen = [UIScreen mainScreen];
    CGRect fullScreenRect = screen.bounds; // always implicitly in Portrait orientation.        

    if (UIInterfaceOrientationIsLandscape(_orientation)) 
    {
        CGRect temp;
        temp.size.width = fullScreenRect.size.height;
        temp.size.height = fullScreenRect.size.width;
        fullScreenRect = temp;      
    }

    return fullScreenRect;
}

- (void)animationFinished:(NSString *)animationID finished:(BOOL)finished context:(void *)context
{   

    if ([animationID isEqualToString:@"expand"])
    {   
    //example
    }
else if ([animationID isEqualToString:@"collapse"])
    {   
    //example
    }
}
+7

UIView ( xib, ) , UIView, .

IBAction, (/). , UIView true. IBAction, , view. , , , , , .

0

Source: https://habr.com/ru/post/1778750/


All Articles