2014-03-13 6 views
0

화면에서 물체 (보기)가 튀는 UI 동적을 구현했습니다. 시뮬레이터에서 완벽하게 작동하지만 실제 아이폰에서 테스트 할 때 경계가 작동하지 않습니다. 필요하다면 나는 코드를 게시 할 것이다. 그러나 이것은 나에게 개념적으로 뭔가 빠져있는 것 같다. 어떤 팁이나 아이디어?UIDynamics 충돌이 실제 아이폰에서 작동하지 않습니다.

추가 세부 정보 : 경계가 화면을 둘러싸고 있으며 시뮬레이터의 두 가지 크기에서 테스트 한 결과 정상적으로 작동합니다.

는 "나쁜" "나쁜"중 하나 칠 때 무슨 일 (인스턴스 변수로 정의 된 또한 "screenWidth"와 "screenHeight")를 view--의 이름

///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)를 기존의 동작을 업데이트하고 싶었처럼 행동 할 것 같다,하지만 당신은 할 때마다 새로운 푸시 동작을 만들 로그에 표시됩니다 . 하나 또는 다른 것을하십시오. 매번 새로운 푸시 동작을 생성하려면 매번 매뉴에 푸시 동작을 추가해야합니다. 한 번만 만들려면 애니메이터에게 한 번만 추가 한 다음 매번 업데이트하고 활성화하십시오. 나는 당신이 당신의 pushDirection 알고리즘을 조정 용서해 희망

- (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; 
} 

참고,하지만 난 당신의 만들기에서 추론 :

당신은 아마 다시 다시, 하나 개의 푸시 동작을 만들어 한 번 추가하고 업데이트 할 (충돌 경계가되는 참조 뷰의 경계를 정의하는 훨씬 쉬운 프로세스가 아닌) 4 개의 벽 중 어떤 벽이 충돌했는지에 따라 푸시 벡터가 달라지기를 원합니다. 상단 벽을 치면 (그리고 그 반대의 경우) 위를 누르면 아래로 밀고 왼쪽 벽을 부딪치면 오른쪽으로 밀린다 (반대의 경우도 마찬가지 임). 그러나 원하는 푸시 벡터 알고리즘을 사용하십시오.

주요 관찰 사항은 하나의 푸시 동작을 추가하고 반복해서 업데이트하려는 경우 또는 매 순간 새로운 푸시 동작을 추가하려는 경우입니다. 하지만 위의 두 시뮬레이터 및 장치에서 작동합니다 (그리고 나는 솔직히 왜 그것이 당신과 당신을 위해 다른 하나의 작동하지 명확하지 않다, 당신의 원래 코드가 둘 중 하나에서 일할 수있는 방법을 모르겠다).

관련 문제