2013-03-27 66 views
2

在我的didSelectRowAtIndexPath方法中,當用戶選擇一個單元格以便在視圖已經被呈現時消除該視圖時,我呼叫[self dismissView];。這顯然不是非常優化的,並且它不覆蓋presentView方法,而沒有動畫dismissView。有一個更好的方法嗎?或者至少讓它等待視圖完成動畫而不使用NSTimer。使用塊排隊動畫

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath; 
{ 
    [self dismissView]; 

// Do xyz... 

[self presentView:twitterLinksView]; 

然後..

- (void)presentView:(id)sender 
{ 
    twitterLinksView.frame = CGRectMake(0, (self.view.frame.size.height + viewHeight), 320, 300); 


    [UIView animateWithDuration:0.60f animations:^{ 


     CGRect twitterLinkFrame = self.twitterLinksView.frame; 
     twitterLinkFrame.origin.y = (self.view.frame.size.height - viewHeight); 

     twitterLinksView.frame = twitterLinkFrame; 
    }]; 
} 

- (void)dismissView 
{ 

    [UIView animateWithDuration:0.75f animations:^{ 

     CGRect twitterLinkFrame = self.twitterLinksView.frame; 
     twitterLinkFrame.origin.y = (self.view.frame.size.height + viewHeight); 

     self.twitterLinksView.frame = twitterLinkFrame; 
    }]; 
} 
+0

動畫結束時可能跟蹤? http://stackoverflow.com/questions/10612389/what-is-the-best-way-to-wait-for-a-loop-of-uiview-animations-to-finish – 2013-03-27 20:58:53

+0

不能說我發現這篇文章有幫助 – justMike 2013-03-27 21:45:51

回答

0

我結束了分裂出我的創作方法和使用[[self.view subviews] containsObject:twitterLinksView]來檢查視圖。另外值得注意的是[self performSelector:@selector(createView) withObject:twitterLinksView afterDelay:0.76f]必須在解散動畫之前稍微超前,以便它可以在所有工作中... yeah wtf。

didSelectRowAtIndexPath方法I中使用[self performSelector:@selector(presentView:)];

由於甲基解決難題的一部分。

- (void)presentView:(id)sender 
{ 

    if ([[self.view subviews] containsObject:twitterLinksView]) 
    { 
     [self dismissView]; 
     [self performSelector:@selector(createView) withObject:twitterLinksView afterDelay:0.76f]; 
     NSLog(@"Animating ut old"); 

    } 
    else 
    { 
     NSLog(@"Creating new view"); 
     [self createView]; 
    } 
} 
-(void)createView 
{ 
    twitterLinksView.frame = CGRectMake(0, (self.view.frame.size.height + viewHeight), 320, 300); 

    [UIView animateWithDuration:0.60f animations:^{ 

     CGRect twitterLinkFrame = self.twitterLinksView.frame; 
     twitterLinkFrame.origin.y = (self.view.frame.size.height - viewHeight); 

     twitterLinksView.frame = twitterLinkFrame; 
     [self.view addSubview:twitterLinksView]; 

    }]; 
} 

- (void)dismissView 
{ 

    [UIView animateWithDuration:0.75f animations:^{ 

     CGRect twitterLinkFrame = self.twitterLinksView.frame; 
     twitterLinkFrame.origin.y = (self.view.frame.size.height + viewHeight); 

     self.twitterLinksView.frame = twitterLinkFrame; 

    }completion:^(BOOL finished){ 
     [twitterLinksView removeFromSuperview]; 
    }]; 
} 
1
[UIView animateWithDuration:1. animations:^{ 

    //firstAnimationBlock 

    } completion:^(BOOL finished){ 
     [UIView animateWithDuration:1. animations:^{ 

//animations in this block will be called after firstAnimationBlock has expired 
     }]; 
    }]; 

據我瞭解,你想火2動畫一前一後。這個代碼(內部塊塊),使該

這部分是編輯後: 現在OK,你可以嘗試寫這樣的

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath; 
{ 
    [self dismissView]; 

// Do xyz... 

[self performSelector:@selector(presentView:) withObject:twitterLinksView afterDelay:0.75]; 

//[self presentView:twitterLinksView]; 
} 
+0

沒有嚴格的說。作爲第一次選擇單元格並且視圖被動畫化。第二次選擇單元格時,視圖需要動畫並用新數據重新生成動畫。 – justMike 2013-03-27 22:00:28

+0

好的。爲你的第二個選項(首先動畫再次),你可以使用這個塊。先動畫頁面,然後重新制作動畫。對於第一個,一個塊就足夠了,除非我明白錯誤 – meth 2013-03-27 22:11:39

+0

根據代碼它應該獨立行動的兩個獨立的行爲,一個不遵循另一個。 – justMike 2013-03-27 23:21:38