2011-12-06 91 views
0

我對IOS開發比較陌生。我有一個關於UIView動畫的查詢。 在我的應用程序中,我有一個視圖說「視圖1」,其中包含三個視圖,標題視圖,視圖2和頁腳視圖。三個視圖中每一個的寬度都相同(與view1相同)。標題視圖和頁腳視圖的高度是固定的,View2的高度取決於View1的高度。因此,View2的高度計算爲View1的高度 - (頁眉視圖的高度+頁腳視圖的高度)。三個子視圖中的每一個的Autoresizing設置爲無。因此,View1的所有子視圖的所有定位內容均在其layoutSubviews方法中完成。視圖1的當前佈局看起來如下UIView動畫與子視圖同步

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

       View1 

現在我想的是,當用戶點擊頁腳視圖,視圖1應與動畫合攏,這樣視圖2的該尺寸變爲零,僅低於頁眉頁腳視圖視圖定位。所以後廠景的動畫當前佈局應該如下

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

      View1 (After Animation) 

爲了達到上述結果,我寫了下面的視圖1

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的高度也應該隨着動畫和頁腳視圖以相同的量減少,並且應該以相同的動畫向上移動,以便輸出結果爲平滑動畫。

任何幫助或方向我怎麼能達到上述結果?

+0

爲什麼不將view2的高度設置爲動畫的一部分? – Kjuly

回答

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]; 
} 
+0

看來你自己創造了一個新項目,好的做法! :p – Kjuly

+0

謝謝skywing它爲我工作:) – Mousam