2012-09-06 71 views
8

在我的應用程序我有一些動畫。例如,我在主菜單中有一個按鈕,當您單擊它時,動畫開始(如移動某個地方等),並在動畫結束時導航到另一個頁面。我需要的是在動畫期間禁用用戶交互。因爲在動畫過程中如果我按下我的按鈕的起點,那麼應該導航的頁面會打開兩次。總結一下,如果我在動畫過程中不讓任何用戶交互,我的問題就會解決。我怎樣才能做到這一點?iphone開發:在動畫過程中禁用用戶交互

+0

你如何動畫的看法?傳統的方法或使用塊? – 2012-09-06 15:07:41

+0

@ NSPostWhenIdle ..爲什麼它錯了或stackoverflow不允許它? – Rajneesh071

+0

好吧,先生...下次我會照顧它,... :) – Rajneesh071

回答

19

動畫之前:

self.view.userInteractionEnabled = NO; 

,並在動畫完成塊:

self.view.userInteractionEnabled = YES; 
+0

如果用戶在視圖上有兩個按鈕,然後wht ..? – Rajneesh071

+1

禁用視圖將禁用所有子視圖 – swebal

+0

是的,你是對的....但我想說,如果用戶有兩個按鈕,並希望同時在兩個按鈕上的一些動畫...那麼怎麼才能根據你的代碼.. ..所以我建議禁用按鈕是更好的方法.... – Rajneesh071

6

很簡單,您可以在動畫開始前將setUserInteractionEnabled設置爲NO,並在動畫完成處理程序中將其設置回YES

[myObject setUserInteractionEnabled:NO]; 
[UIView animateWithDuration:1.0 animations:^{ 
    [myObject setTransform:CGAffineTransformMakeTranslation(100, 100)];//some animation 
}completion:^(BOOL done){ 
    if (done){ 
     [myObject setUserInteractionEnabled:YES]; 
    } 
}]; 
2
yourView.userInteractionEnabled = NO; 
[UIView animateWithDuration:1 animations:^ 
{ 
    //animations here      
} 
completion:^(BOOL finished) 
{ 
    yourView.userInteractionEnabled = YES; 
}]; 
1

按鈕的禁用userIntrection。

Btn.userInteractionEnabled = NO; 
5

你不必砍周圍建成塊 - 有一個動畫選項,它不只是這正是:

[UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction 
    animations:^{ 
     // animations here 
    } 
    completion:nil]; 

如果您設置了UIViewAnimationOptionAllowUserInteraction,那麼用戶交互將是允許。

+1

問題是如何在動畫期間禁用用戶交互的所有內容。這個動畫選項允許用戶交互視圖動畫。 – AWrightIV

15

這可能幫助:

// for ignoring event 
[[UIApplication sharedApplication] beginIgnoringInteractionEvents]; 
[[UIApplication sharedApplication] endIgnoringInteractionEvents]; 

代碼如下:

[UIView animateWithDuration:1.0 animations:^{ 
     //some animation 
     [[UIApplication sharedApplication] beginIgnoringInteractionEvents]; 
    } 
    completion:^(BOOL done){ 
     if (done){ 
      [[UIApplication sharedApplication] endIgnoringInteractionEvents]; 
     } 
    } 
]; 
+1

正是我需要的,謝謝! – iceydee

2

在視圖中禁用觸摸事件,

[[UIApplication sharedApplication] beginIgnoringInteractionEvents]; 

在視圖中

使觸摸事件
[[UIApplication sharedApplication] endIgnoringInteractionEvents]; 
0

我有視圖控制器打開頁面的圖標。 如果用戶快速點擊icon1和icon2,則打開2個頁面。

防止我有這個2號線到水龍頭事件 年初這確保無論發生,endIgnoring將調用

-(void) on_image_tap:(UITapGestureRecognizer *) tapGesture 
{ 
    [[UIApplication sharedApplication] beginIgnoringInteractionEvents]; 
    [[UIApplication sharedApplication] performSelector:@selector(endIgnoringInteractionEvents) withObject:nil afterDelay:0.5f];