我很抱歉,如果这看起来像一个基本问题,但似乎我的谷歌搜索技能不能胜任找到答案的任务,我希望你能帮忙!
基本上,我希望我的应用程序有一个按钮或一个标签,然后单击它会在其下方显示一些文本(以常见问题解答的形式,如此链接:http://aptow.com/faq)并将所有内容向下推用它。我知道如何点击并显示之前隐藏的元素,但我不知道如何做到这种下滑效果,同时在点击时将所有其他内容推到屏幕下方,然后重新一旦用户再次点击以重新隐藏以前隐藏的内容,将其排列在屏幕顶部附近。我给出的链接显示了我正在寻找的内容。
有什么想法吗?提前谢谢。
答案 0 :(得分:1)
正如@hermannKlecker所说,你可以使用animateWithDuration:animations:
这个方法来阻止这个[view animateWithDuration:duration animations:^{ //Block code here
/*set the new positions of the view either move up or down*/
}];
此代码应该这样做。
答案 1 :(得分:1)
如果您想要发布类似于发布链接的FAQ页面,可以使用动态单元格高度的UITableView。
一个简单的UITableViewController看起来像这样:
#import "ViewController.h"
@interface ViewController () {
NSArray *data;
int selected;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
selected = -1;
data = [NSArray arrayWithObjects:
[NSDictionary dictionaryWithObjectsAndKeys:@"Title 1", @"title", @"Lorem ipsum dolor bla blab bla", @"details", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Title 2", @"title", @"Lorem ipsum dolor bla blab bla", @"details", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Title 3", @"title", @"Lorem ipsum dolor bla blab bla", @"details", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Title 4", @"title", @"Lorem ipsum dolor bla blab bla", @"details", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Title 5", @"title", @"Lorem ipsum dolor bla blab bla", @"details", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Title 6", @"title", @"Lorem ipsum dolor bla blab bla", @"details", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Title 7", @"title", @"Lorem ipsum dolor bla blab bla", @"details", nil],nil ];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == selected) {
return 130;
} else
return tableView.rowHeight;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [data count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"ci";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
NSDictionary *tmp = [data objectAtIndex:indexPath.row];
cell.textLabel.text = nil;
static int TITLE_LABEL_TAG = 122;
UILabel *titleLabel = (UILabel *)[cell.contentView viewWithTag:TITLE_LABEL_TAG];
if (!titleLabel) {
titleLabel = [[UILabel alloc] initWithFrame:CGRectNull];
titleLabel.tag = TITLE_LABEL_TAG;
[cell.contentView addSubview:titleLabel];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.numberOfLines = 1;
titleLabel.font = [UIFont boldSystemFontOfSize:16];
[cell.contentView addSubview:titleLabel];
}
titleLabel.frame = (CGRectMake(8, 8, cell.contentView.frame.size.width-16, 20));
titleLabel.text = [tmp objectForKey:@"title"];
static int DETAIL_LABEL_TAG = 123;
UILabel *detailLabel = (UILabel *)[cell.contentView viewWithTag:DETAIL_LABEL_TAG];
if (!detailLabel) {
detailLabel = [[UILabel alloc] initWithFrame:CGRectNull];
detailLabel.tag = DETAIL_LABEL_TAG;
[cell.contentView addSubview:detailLabel];
detailLabel.backgroundColor = [UIColor clearColor];
detailLabel.numberOfLines = 0;
}
if (indexPath.row == selected) {
detailLabel.frame = (CGRectMake(8, 30, cell.contentView.frame.size.width-16, 100));
detailLabel.hidden = NO;
detailLabel.text = [tmp objectForKey:@"details"];
} else {
detailLabel.hidden = YES;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSMutableArray *ip2reload = [NSMutableArray array];
if (selected > -1 && selected != indexPath.row)
[ip2reload addObject:[NSIndexPath indexPathForRow:selected inSection:0]];
[ip2reload addObject:[NSIndexPath indexPathForRow:indexPath.row inSection:0]];
selected = indexPath.row;
[tableView reloadRowsAtIndexPaths:ip2reload withRowAnimation:UITableViewRowAnimationAutomatic];
}
@end
您肯定必须根据内容计算所选行的实际行高(例如,如此处所述:Iphone - when to calculate heightForRowAtIndexPath for a tableview when each cell height is dynamic?)。
你可以使用scrollToRowAtIndexPath:atScrollPosition:animated来确保所选单元格正好在你的屏幕上。
答案 2 :(得分:1)
您可以通过设置视图框架属性的动画来实现此目的。
使用适合其初始内容(即标签)的框架高度初始化的UIView(或子类,如UITextView),如果使用单独的按钮或标签切换,则不执行任何操作可见性)。
然后确定显示所需扩展内容所需的帧高度,并设置为此新高度的动画。下面的其他观点(如果有的话)的位置也必须改变相同的数量。
作为示例:
[UIView beginAnimations:@"expand" context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelegate:self];
[myView setFrame:CGRectMake(myView.frame.origin.x, myView.frame.origin.y, myView.frame.size.width, expandedHeight)]; // set expandedHeight to the height you require
for (UIView *view in myViews) { // myViews is an array containing each view that can be expanded or moved.
if (view.frame.origin.y > myView.frame.origin.y) // check if view is below the one to be expanded
[view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y + expandedHeight, view.frame.size.width, view.frame.size.height)]; // animate the move
}
[UIView commitAnimations];
还有其他各种方法可以为UIView及其子类的框架设置动画,请检查Apples UIView Reference。