我为屏幕周围的对象(视图)实现了UIdynamics。在模拟器上,这可以完美地工作,但是当在实际的iphone上进行测试时,边界不起作用。我会在必要时发布代码,但在我看来,这就像我在概念上遗漏了一些东西。任何提示或想法?
其他细节:屏幕周围的边界,我已经测试了两种尺寸的模拟器,它工作正常。
“bad”是视图的名称 - (“screenWidth”和“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;
甚至print语句也不会显示在日志中
答案 0 :(得分: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
算法,但是我从你创建的四面墙中推断出来(而不是将参考视图的边界定义为碰撞边界的更容易的过程) )你希望推动矢量根据碰撞的墙壁而变化。如果你撞到顶墙(反之亦然),上面会向下推,如果你撞到左墙,则向右推(反之亦然)。但是使用你想要的任何推送矢量算法。
主要观察是你想要添加一个推送行为并一次又一次地更新它,或者你想每次只添加新的瞬时推送行为。但是上面的工作在模拟器和设备上(我老实说不清楚为什么它适用于你而不是另一个;我不知道你的原始代码是如何工作的。)