3

如果这似乎是一个基本问题,我很抱歉,但我的谷歌搜索技能似乎无法找到我的答案,我希望你能提供帮助!

基本上,我希望我的应用程序有一个按钮或一个标签,点击它会在其下方显示一些文本(以常见问题解答的形式,如这里的链接:http: //aptow.com/faq)并推送所有内容放下它。我知道如何单击并显示以前隐藏的元素,但我不确定如何实现向下滑动的效果,同时在单击时将其他所有内容推到屏幕下方,然后重新一旦用户再次单击以重新隐藏以前隐藏的内容,将其安排在屏幕顶部附近。我给出的链接正是我想要做的。

有任何想法吗?提前致谢。

4

3 回答 3

1

正如@hermannKlecker 所说,您可以使用 animateWithDuration:animations 方法对块执行此操作:

[view animateWithDuration:duration animations:^{ //Block code here
/*set the new positions of the view either move up or down*/
}];

这段代码应该这样做。

于 2012-08-22T08:40:14.333 回答
1

如果您想做类似于您发布链接的常见问题解答页面的操作,您可以使用 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 - 当每个单元格高度为动态时,何时计算 tableview 的 heightForRowAtIndexPath?)。

您可以使用 scrollToRowAtIndexPath:atScrollPosition:animated 来确保所选单元格位于屏幕顶部。

于 2012-08-22T08:48:07.423 回答
1

您可以通过为视图的 frame 属性设置动画来做到这一点。

使用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

于 2012-08-22T08:57:46.383 回答