如何创建可折叠的UIView

时间:2010-12-06 14:28:12

标签: iphone objective-c cocoa

我需要创建某种可扩展且可折叠的UIView,并且无法支付第三方控件的费用。

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

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

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

有没有人有任何想法如何实现这一点 - 这里的noob :(

2 个答案:

答案 0 :(得分:7)

对一个ViewController进行子类化,并且你的按钮可以触发两个方法,如'collapse'和'expand',这可能会让你开始:你可以动态地为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
    }
}

答案 1 :(得分:0)

您可以尝试在屏幕外添加UIView(以xib或编程方式),然后使用UIView动画将其滑入主要查看区域。

该按钮将具有IBAction,可以切换动画(滑入/滑出)。您可以将其滑出屏幕,然后将UIView的隐藏属性设置为true。当您回想起IBAction时,请检查视图的隐藏属性是否为真。如果是,你知道播放滑动的动画,如果没有,那么你就播放滑出的动画。