iphone - 如何创建可折叠的 UIView

标签 iphone objective-c cocoa

我需要创建某种可扩展和可折叠的 UIView,但无法为第三方控件付费。

这基本上是我希望它的行为方式:

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

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

有没有人知道如何简单地实现这个 - 菜鸟:(

最佳答案

子类化一个 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
    }
}

关于iphone - 如何创建可折叠的 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4367390/

相关文章:

objective-c - 我应该如何弹出到特定的 View Controller ?

iphone - 如何在 iPhone 上使用核心图实现股票成交量图表?

iphone - 使用 NSDateFormatter 解析 Twitter 字符串

ios - 无法在 detailTextLabel 中显示 NSString

cocoa - Web View 更改字体: and changeColor: with no selection reset changes when i type

objective-c - 使用 Restkit 为核心数据创建临时对象的更好方法

iphone - 添加文本到图像

ios - 在 [NSString stringWithContentsOfURL :url]; 之后,选项卡 Controller 卡住并且手机变得迟钝

ios - Xcode 5 中 libzbar.a 的链接器错误

objective-c - Command/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/clang++ 失败,退出代码为 1