2013-01-09 158 views
2

我遇到了UIView子視圖動畫的一些問題。我想要做的是,當你按下主視圖時,子視圖將從頂部滑下,然後在下一個水龍頭上滑動並移除。但是在當前狀態下,它只是執行第一次敲擊命令,而在第二次敲擊時,它會顯示一個nslog,但視圖和動畫的刪除不起作用。UIView動畫無法正常工作

這裏是事件處理函數的代碼:

- (void)tapGestureHandler: (UITapGestureRecognizer *)recognizer 
{ 
NSLog(@"tap"); 

CGRect frame = CGRectMake(0.0f, -41.0f, self.view.frame.size.width, 41.0f); 
UIView *topBar = [[UIView alloc] initWithFrame:frame]; 
UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"topbar.png"]]; 
topBar.backgroundColor = background; 

if (topBarState == 0) { 
    [self.view addSubview:topBar]; 
    [UIView animateWithDuration:0.5 animations:^{topBar.frame = CGRectMake(0.0f, 0.0f, self.view.frame.size.width, 41.0f);}]; 
    topBarState = 1; 
} else if (topBarState == 1){ 
    [UIView animateWithDuration:0.5 animations:^{topBar.frame = CGRectMake(0.0f, -41.0f, self.view.frame.size.width, 41.0f);} completion:^(BOOL finished){[topBar removeFromSuperview];}]; 

    NSLog(@"removed"); 
    topBarState = 0; 
} 

} 

我如何讓這個子視圖生命和正常刪除嗎?

問候

FreeSirenety

回答

2

你總是設定Y = -41頂欄架,所以topBarState = 1,動畫作品爲y=-41 to y=-41,似乎是不工作

CGRect frame = CGRectMake(0.0f, -41.0f, self.view.frame.size.width, 41.0f); 
UIView *topBar = [[UIView alloc] initWithFrame:frame]; 

每次您創建視圖topBar時。
在.h中聲明topBar並在viewDidLoad中分配init。

- (void)viewDidLoad { 
    CGRect frame = CGRectMake(0.0f, -41.0f, self.view.frame.size.width, 41.0f); 
    topBar = [[UIView alloc] initWithFrame:frame]; 
    UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"topbar.png"]]; 
    topBar.backgroundColor = background; 
     [self.view addSubview:topBar]; 
    topBarState = 0; 
} 

- (void)tapGestureHandler: (UITapGestureRecognizer *)recognizer 
{ 
    if (topBarState == 0) { 
      [UIView animateWithDuration:0.5 animations:^{topBar.frame = CGRectMake(0.0f, 0.0f, self.view.frame.size.width, 41.0f);}]; 
      topBarState = 1; 
    } else if (topBarState == 1){ 
     [UIView animateWithDuration:0.5 animations:^{topBar.frame = CGRectMake(0.0f, -41.0f, self.view.frame.size.width, 41.0f);} completion:^(BOOL finished){[topBar removeFromSuperview];}]; 
     topBarState = 0; 
    } 
} 
+0

感謝您的回答,使其完美工作! :) – FreeSirenety