2014-02-20 60 views
4

我使用Apple的示例應用程序中的這段代碼,並將其調整爲我的。我想UIImageView gameOverFalling運行此方法(秋季和反彈的種類)。它應該與UIView的finalScoreView碰撞,你可以在第2行UIDynamicAnimator - 不會與UIView一起使用,但會與Self.View一起使用?

-(void)gameOverFallingMethod{ 
    UIDynamicAnimator *animator = [[UIDynamicAnimator alloc] initWithReferenceView:finalScoreView]; 

    UIGravityBehavior *gravityBeahvior = [[UIGravityBehavior alloc] initWithItems:@[self.gameOverFalling]]; 
    [animator addBehavior:gravityBeahvior]; 

    UICollisionBehavior *collisionBehavior = [[UICollisionBehavior alloc] initWithItems:@[self.gameOverFalling]]; 
    // Apple said: "Creates collision boundaries from the bounds of the dynamic animator's 
    // reference view (self.view)." 
    collisionBehavior.translatesReferenceBoundsIntoBoundary = YES; 
    collisionBehavior.collisionDelegate = self; 
    [animator addBehavior:collisionBehavior]; 

    self.animator = animator; 
} 

然而,當我跑我的應用程序,它返回一個線程1看到:SIGABRT錯誤。在控制檯:

View item (<UIImageView: 0x10aaa0c70; frame = (15 175; 288 42); 
    autoresize = RM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x10aa7e0a0>>) 
should be a descendant of reference view in <UIDynamicAnimator: 0x10ae17610> 
Stopped (0.000000s) in <UIView: 0x10aa9e1e0> {{0, 0}, {288, 195}} 

它工作時,我代替「finalScoreView」與「self.view」 2號線,但隨後下降到整個屏幕的底部。

任何想法我做錯了什麼?我很想知道,謝謝!

回答

6

從例外情況看,self.gameOverFalling不會從您的視圖層次結構中的finalScoreView下降。從UIDynamicAnimator class reference見報價:

要動態視圖,創建與initWithReferenceView的漫畫家: 方法。參考視圖的座標系用作動畫師行爲和項目的座標系。與這類動畫製作器關聯的每個動態 項必須是UIView對象 ,並且必須從參考視圖下降。

1

你可能忘了你的項目添加到使用參照視點:

finalScoreView.addSubview(@[self.gameOverFalling]) 
2

我解決了這個通過添加添加任何行爲之前,子視圖。

 addSubview(square) 
     var gravity = UIGravityBehavior(items: [square]) 
     gravity.magnitude = gravitySpeed 
     animator.addBehavior(gravity) 
+0

它可以解決您的問題! – Fabio

相關問題