我们的目标是创造一个环境,一个球的图像将在一个视图中非常缓慢地不断反弹。我可以使无休止的弹跳以高速工作,但不能以慢速工作。
这是代码示例:
UIDynamicAnimator *animator = [[UIDynamicAnimator alloc] initWithReferenceView: self.view];
UICollisionBehavior *ballCollision = [[UICollisionBehavior alloc] initWithItems: @[largeBall, mediumBall, smallBall]];
[ballCollision setTranslatesReferenceBoundsIntoBoundary: YES];
[ballCollision setCollisionMode: UICollisionBehaviorModeBoundaries];
float force = 0.1;
int angle = 225;
NSNumber *rad = [NSNumber numberWithDouble: DEGREES_TO_RADIANS(angle)];
UIPushBehavior *smallPush = [[UIPushBehavior alloc] initWithItems: @[smallBall] mode: UIPushBehaviorModeInstantaneous];
[smallPush setAngle: [rad floatValue]];
[smallPush setMagnitude: force];
UIDynamicItemBehavior *dynamicBehavior = [[UIDynamicItemBehavior alloc] initWithItems: @[smallBall]];
[dynamicBehavior setDensity: 0.0];
[dynamicBehavior setElasticity: 1.0];
[dynamicBehavior setFriction: 0.0];
[dynamicBehavior setResistance: 0.0];
[animator addBehavior: ballCollision];
[animator addBehavior: smallPush];
[animator addBehavior: dynamicBehavior];
我的高中物理课一定没有沉没,因为我不明白为什么这不起作用。摩擦力为零,弹性为一,球应该永远弹跳,不是吗?