UIView动画与子视图同步

时间:2011-12-06 15:16:53

标签: iphone objective-c animation uiview

我对IOS开发相对较新。我有关于UIView动画的查询。 在我的应用程序中,我有一个视图说“View1”,它包含三个视图,标题视图,视图2和页脚视图。三个视图中的每一个的宽度相同(与view1的宽度相同)。标题视图和页脚视图的高度是固定的,View2的高度取决于View1的高度。所以View2的高度计算为View1的高度 - (标题视图的高度+页脚视图的高度)。三个子视图中每个子视图的自动调整设置为无。因此,View1的所有子视图的所有定位内容都在其layoutSubviews方法中完成。 View1的当前布局如下所示

    -------------------------
    |      Header View      |
    |                       |
    -------------------------
    |                       |
    |                       |
    |       View2           |
    |                       |
    -------------------------
    |    Footer View        |
    |                       |
    -------------------------

                View1

现在我想要的是当用户点击页脚视图时,view1应该使用动画折叠,以便View2的大小变为零,页脚视图位于标题视图的正下方。因此,动画当前的view1布局应如下所示

            -------------------------
            |    Header View        |
            |                       |
            -------------------------
            |    Footer View        |
            |                       |
            -------------------------

             View1 (After Animation)

为了达到上述结果,我在View1的父视图中在页脚视图上的点击手势的处理程序方法中编写了以下代码

    CGFloat headerViewHeight = self.view1.headerView.frame.size.height;
    CGFloat footerViewHeight = self.view1.footerView.frame.size.height;
    CGFloat newHeight = (headerViewHeight + footerViewHeight);
    CGRect frame = self.view1.frame;
    frame.size.height = newHeight;

    [UIView beginAnimation:nil context:NULL];
    [UIView setAnimationDuration:3.0f];

    self.view1.frame = frame;

    [UIView commitAnimation];

在运行上面的代码时,在屏幕上,View2的高度设置为0,页脚视图立即位于标题视图正下方,然后View1的高度随着动画向newHeight动画缓慢减少。这不是理想的结果,我想要的是当View1的高度随着动画减少时View2的高度也应该随着动画的减少相同而且页脚视图应该向上移动相同数量的动画,因此输出会产生平滑的动画

有关如何达到上述结果的任何帮助或指示?

1 个答案:

答案 0 :(得分:1)

以下是适合我的代码段。希望它能给你一些想法。

@implementation MasterView
{
    UIView *headerView;
    UIView *view2;
    UIView *footerView;
    bool collapsed;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor yellowColor];

        headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
        headerView.backgroundColor = [UIColor redColor];

        view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 50, 320, 100)];
        view2.backgroundColor = [UIColor greenColor];

        footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 150, 320, 100)];
        footerView.backgroundColor = [UIColor blueColor];
        footerView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;

        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toggle:)];
        [footerView addGestureRecognizer:tapGesture];   
    }
    return self;
}

- (void)toggle:(UITapGestureRecognizer *)recognizer
{
    [UIView animateWithDuration:1.0 animations:^{
        CGRect view2Frame = view2.frame;
        view2Frame.size.height = 0;
        view2.frame = view2Frame;

        CGRect selfFrame = self.frame;
        selfFrame.size.height -= 100;
        self.frame = selfFrame;
    }];
}

-(void)layoutSubviews
{
    [self addSubview:headerView];
    [self addSubview:view2];
    [self addSubview:footerView];
}