2010-09-01 35 views
14

我有一個UIScrollView子類,我用UIView動畫編程滾動。UIScrollView觸發事件期間動畫不animateWithDuration射擊:但工作正常與UIView beginAnimations:

我希望用戶能夠點擊或放大Scroll View的UIImageView內容,而動畫正在發生

- (void) scrollSmoothlyatPixelsPerSecond:(float)thePixelsPerSecond { 
    // distance in pixels/speed in pixels per second 
    float animationDuration = _scrollView.contentSize.width/thePixelsPerSecond; 

    [UIView beginAnimations:@"scrollAnimation" context:nil]; 
    [UIView setAnimationCurve: UIViewAnimationCurveLinear]; 
    [UIView setAnimationDuration:animationDuration]; 
    _scrollView.contentOffset = CGPointMake(_scrollView.contentSize.width, 0); 
    [UIView commitAnimations]; 
} 

現在,由於iOS的4.0,UIView的beginAnimations:不鼓勵

這在使用類似於此的配方一直很好。所以我嘗試使用塊和UIView animateWithDuration更新我的代碼:滾動的工作方式與上述相同。

的關鍵和令人發狂的區別是,動畫的過程中,UIScrollView的和其他的觀點沒有更長的時間事件處理方法作出迴應:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event; 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event; 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event; 

也不:

-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView; 

在嘗試時調用放大。

編輯爲了清楚起見:沒有UIView禮物響應觸摸事件。這不僅限於UIScrollView。 UIScrollView的對等UIToolbar不響應觸摸事件,也不響應UIScrollView的對等子視圖。看起來整個父級UIView被凍結在用戶交互之外,而動畫正在進行。再次,動畫完成後,所有上面的UIViews都會再次響應。

這些都是在UIView beginAnimations:中調用的,不管動畫狀態如何。

我的animateWithDuration:代碼稍有不同 - 但差異不重要。一旦動畫完成,上面的觸摸事件再次呼籲......

這裏是我的動畫代碼:

- (void) scrollSmoothlyToSyncPoint:(SyncPoint *) theSyncPoint andContinue:(BOOL)theContinueFlag{ 

    float animationDuration = theSyncPoint.time - [player currentTime]; 
    [UIView animateWithDuration:animationDuration 
          delay:0 
         options:UIViewAnimationOptionCurveLinear 
        animations:^{ 
     [_scrollView setContentOffset:theSyncPoint.contentOffset]; 
    } 
        completion:^(BOOL finished){ 
         if (theContinueFlag) { 
          SyncPoint *aSyncPoint = [self nextSyncPoint]; 
          if (aSyncPoint) { 
           [self scrollSmoothlyToSyncPoint:aSyncPoint andContinue:YES]; 
          } 
         } 
    }]; 
} 

唯一的事件處理程序,上面的動畫塊中的火災是:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event; 

所以,問題:我應該 - 忽視蘋果的氣餒標籤,並繼續使用beginAnimation配方?我應該 - 使用hitTest重新實現縮放和其他基於觸摸的事件嗎?是否有一些關於動畫塊之間實現差異的知識可以幫助我解決這個問題?有什麼明顯的我失蹤了?

我是蘋果的一位新開發者,所以我不知道如何認真對待阻止標籤。但是,如果這個API將被棄用,然後消失,我寧願移動一個持久的方向。

非常感謝您的關注。

+0

這種情況真的會出現由低內存情況進行提示。我沒有時間全面測試這是否確實是原因(這就是爲什麼我還沒有回答這個問題)。但是,在低內存情況下遇到類似的事件處理暫停之後,它確實會出現這種情況。現在 - 爲什麼基於塊的動畫會受到影響,而另一個則不會......當我得到時間和帖子時,我會運行一個全面的測試。 – Jackifus 2010-09-29 19:32:57

回答

41

你只需要包括UIViewAnimationOptionAllowUserInteraction選項調用動畫像這樣的時候:

[UIView animateWithDuration:animationDuration 
          delay:0 
         options:(UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction) 
        <snip>]; 

這是一個狡猾的一個,我知道了! ;)

回覆:「望而卻步」的標籤 - 一般情況下,當蘋果告訴你不要做的問候東西,設計出自己的平臺上運行的應用程序,它通常是爲自己好。所以你是想採用動畫塊而不是笨重的舊方式。

+0

確實偷偷摸摸 - 謝謝mucho! – Jackifus 2010-10-09 23:16:23

+0

等待,你的意思是基於塊的動畫是BLOCKing?這就像一個代碼雙關語! (對不起,無法抗拒。+1一個非常有用的答案) – Bdebeez 2011-06-28 13:13:10

+0

出色的答案,非常感謝! – joern 2011-07-21 11:53:07

相關問題