2014-03-13 19 views
0

我實現了在屏幕上彈跳的對象(視圖)的UIdynamics。在模擬器上,它可以完美地工作,但是當在真正的iPhone上測試時,邊界不起作用。如果有必要,我會發布代碼,但在我看來,就像我在概念上缺少某些東西。任何提示或想法?UIDynamics碰撞無法在物理iphone上運行

附加細節:屏幕周圍的邊界,我已經測試了兩種尺寸的模擬器,它工作正常。

「壞」是view--的名稱(也稱爲「屏幕寬度」和「screenHeight」已被定義爲實例變量)

///left wall 
badCollision = [[UICollisionBehavior alloc] initWithItems:@[bad]]; 
badCollision.collisionDelegate = self; 
CGPoint pt1 = CGPointMake(0, 0); 
CGPoint pt2 = CGPointMake(0, screenHeight); 
[badCollision addBoundaryWithIdentifier:@"leftWall" fromPoint:pt1 toPoint:pt2]; 
[animator addBehavior:badCollision]; 

//right wall 
badCollision = [[UICollisionBehavior alloc] initWithItems:@[bad]]; 
badCollision.collisionDelegate = self; 
pt1 = CGPointMake(screenWidth, 0); 
pt2 = CGPointMake(screenWidth, screenHeight); 
[badCollision addBoundaryWithIdentifier:@"rightWall" fromPoint:pt1 toPoint:pt2]; 
[animator addBehavior:badCollision]; 

//top wall 
badCollision = [[UICollisionBehavior alloc] initWithItems:@[bad]]; 
badCollision.collisionDelegate = self; 
pt1 = CGPointMake(0, 0); 
pt2 = CGPointMake(screenWidth, 0); 
[badCollision addBoundaryWithIdentifier:@"topWall" fromPoint:pt1 toPoint:pt2]; 
[animator addBehavior:badCollision]; 

//bottom wall 
badCollision = [[UICollisionBehavior alloc] initWithItems:@[bad]]; 
badCollision.collisionDelegate = self; 
pt1 = CGPointMake(0, screenHeight); 
pt2 = CGPointMake(screenWidth, screenHeight); 
[badCollision addBoundaryWithIdentifier:@"bottomWall" fromPoint:pt1 toPoint:pt2]; 
[animator addBehavior:badCollision]; 

這裏是當「壞」的歌曲之一,會發生什麼牆壁。

NSLog(@"Wall Hit"); 
    UIPushBehavior *badForce = [[UIPushBehavior alloc] initWithItems:@[item] mode:UIPushBehaviorModeInstantaneous]; 
    UIView *itemView = (UIView*)item; 
    itemView.backgroundColor = [UIColor redColor]; 
    [UIView animateWithDuration:0.3 animations:^{ 
     itemView.backgroundColor = [UIColor blueColor]; 
    }]; 
    int xneg = (int)drand48(); 
    if (xneg < .51) 
     xneg = 1; 
    else 
     xneg = -1; 
    int yneg = (int)drand48(); 
    if (yneg < .51) 
     yneg = 1; 
    else 
     yneg = -1; 
    double xSpeed = xneg*(drand48()+1)/20+.02; 
    double ySpeed = yneg*(drand48()+1)/20+.02; 
    badForce.pushDirection = CGVectorMake(xSpeed,ySpeed); 
    badForce.active = YES; 

甚至沒有打印賬單會顯示在日誌中

+0

你的權利,回想起來,這似乎很模糊。有多個視圖可以繪製20x20的矩形。這些以隨機方向施加的瞬時力開始。這些視圖沒有阻力,重力或摩擦。我更新了上面的代碼以顯示爲視圖創建的邊界。我還補充說碰撞事件會發生什麼。 – MingMan

回答

0

你似乎表現得就像你想更新現有的行爲(你badForce.active = YES),但你每次創建新推的行爲。做一個或另一個。如果您每次都要創建新的推送行爲,則必須每次將其添加到動畫製作人員。如果您要創建一次,請將其添加到您的漫畫家,然後每次更新並激活它。

你可能想一次又一次地創造一個推動的行爲,加一次,並對其進行更新:

- (void)collisionBehavior:(UICollisionBehavior *)behavior beganContactForItem:(id<UIDynamicItem>)item withBoundaryIdentifier:(id<NSCopying>)identifier atPoint:(CGPoint)p 
{ 
    NSString *wallIdentifier = (id)identifier; 
    NSLog(@"Wall Hit: %@", wallIdentifier); 

    if (!self.push) { 
     self.push = [[UIPushBehavior alloc] initWithItems:@[item] mode:UIPushBehaviorModeInstantaneous]; 
     self.push.active = NO; 
     [self.animator addBehavior:self.push]; 
    } 

    UIView *itemView = (UIView*)item; 
    itemView.backgroundColor = [UIColor redColor]; 
    [UIView animateWithDuration:0.3 animations:^{ 
     itemView.backgroundColor = [UIColor blueColor]; 
    }]; 

    double weight = 0.5; 
    double xSpeed = weight * (drand48() * 2.0 - 1.0); // rand number between -weight and weight 
    double ySpeed = weight * (drand48() * 2.0 - 1.0); // rand number between -weight and weight 

    if ([wallIdentifier isEqualToString:@"bottomWall"]) 
     ySpeed = -fabs(ySpeed); 
    else if ([wallIdentifier isEqualToString:@"topWall"]) 
     ySpeed = fabs(ySpeed); 
    else if ([wallIdentifier isEqualToString:@"leftWall"]) 
     xSpeed = fabs(xSpeed); 
    else if ([wallIdentifier isEqualToString:@"rightWall"]) 
     xSpeed = -fabs(xSpeed); 

    self.push.pushDirection = CGVectorMake(xSpeed,ySpeed); 
    self.push.active = YES; 
} 

注意,我希望你能原諒我調整您pushDirection算法,但我從你創建所推斷(而不是更簡單的將參考視圖的邊界定義爲碰撞邊界的過程),您希望推送矢量根據碰巧碰撞的牆壁而變化。如果您碰到頂牆(以及反之亦然),則上述按鈕會向下推動,如果碰到左側牆壁則會向右推動(反之亦然)。但是使用你想要的任何推向量算法。

主要觀察結果是,您要麼添加一個推動行爲並一次又一次地更新它,要麼每次只添加新的即時推動行爲。但是,上面的作品在模擬器和設備上都是這樣(我真的不清楚它爲什麼在一個模式和另一個模式上工作,我沒有看到你的原始代碼是如何工作的)。