我需要创建某种可扩展和可折叠的 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/