2013-04-01 35 views
0

我發現下面列出的代碼從https://github.com/DuncanMC/iOS-CAAnimation-group-demo。這種特殊的方法允許用戶使用手勢識別器在覈心動畫序列中「在飛行中」時停止UIView。點擊視圖時,動畫停止。如圖所示,此代碼僅適用於動畫視圖。我有很多動畫視圖,我需要與任何視圖進行交互。我想我必須設置一系列視圖(或圖層)並循環瀏覽它們。它是否正確?我怎麼能這樣做?謝謝!如何設置手勢識別器在所有視圖都動畫時與任何UIView進行交互?

/* 
This method gets called from a tap gesture recognizer installed on the view myContainerView. 
We get the coordinates of the tap from the gesture recognizer and use it to hit-test 
myContainerView.layer.presentationLayer to see if the user tapped on the moving image view's 
(presentation) layer. The presentation layer's properties are updated as the animation runs, so hit-testing 
the presentation layer lets you do tap and/or collision tests on the "in flight" animation. 
*/ 

- (IBAction)testViewTapped:(id)sender 
{ 
    CALayer *tappedLayer; 
    id layerDelegate; 
    UITapGestureRecognizer *theTapper = (UITapGestureRecognizer *)sender; 
    CGPoint touchPoint = [theTapper locationInView: myContainerView]; 
    if (animationInFlight) 
{ 
    tappedLayer = [myContainerView.layer.presentationLayer hitTest: touchPoint]; 
    layerDelegate = [tappedLayer delegate]; 

    if (((layerDelegate == imageOne && !doingMaskAnimation)) || 
     (layerDelegate == waretoLogoLarge && doingMaskAnimation)) 
    { 
     if (myContainerView.layer.speed == 0) 
     [self resumeLayer: myContainerView.layer]; 
     else 
    { 
     [self pauseLayer: myContainerView.layer]; 

    //Also kill all the pending label changes that we set up using performSelector:withObject:afterDelay 
    [NSObject cancelPreviousPerformRequestsWithTarget: animationStepLabel]; 
     } 
    } 
    } 
} 

回答

0

LOL。該演示項目是我的。編寫代碼來查找被挖掘的圖層,然後使用這樣的事實:對於支持UIView的圖層,圖層的委託是視圖本身。

在找到layerDelegate的代碼中,您應該確保它是KindOfClass UIView,然後使用適當的方法將您的視圖與動畫視圖進行匹配。您可以使用IBOutletCollection跟蹤您正在動畫的視圖,或者手動創建可變數組並向其添加視圖對象,使用視圖標籤或任何對您的應用程序有意義的內容。

相關問題