如何创建可折叠的UIView

The*_*ner 3 iphone cocoa objective-c

我需要创建一种可扩展且可折叠的UIView,并且无法为第三方控件付费.

这基本上是我希望它表现的方式:

在顶部应该有一个UIButton(或类似的),允许用户在展开和折叠之间切换.

扩展时我希望能够放置其他UIView(例如日历),当折叠时,周围的控件应该向上移动.

有没有人有任何想法如何实现这简单 - noob在这里:(

Luk*_*ice 7

对ViewController进行子类化,并且有两个按钮可以触发的方法,如'collapse'和'expand',这可能会让你开始:你可以动态地为UIButtons分配新的选择器:

[button addTarget:self action:@selector(eventMethod:)
     forControlEvents:UIControlEventTouchUpInside];
Run Code Online (Sandbox Code Playgroud)

码:

#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
    }
}
Run Code Online (Sandbox Code Playgroud)