当用户摇动设备时,我想用它在屏幕上的物体内晃动。 我假设我需要设置一个隐形框才能与之碰撞。如果它随机移动或遵循预定义的路径无关紧要,无论哪个最简单。
我想我理解“激活摇动”部分代码,只是球/物体运动我不确定
谢谢:)
答案 0 :(得分:4)
这应该有效:
//You need an @property (nonatomic, strong) UIDynamicAnimator *animator; in your .h
self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.viewToBounceAroundIn];
UICollisionBehavior *collision = [[UICollisionBehavior alloc] initWithItems:@[self.viewThatBouncesAround]];
collision.translatesReferenceBoundsIntoBoundary = YES;
[self.animator addBehavior:collision];
UIPushBehavior *push = [[UIPushBehavior alloc] initWithItems:@[self.viewThatBouncesAround] mode:UIPushBehaviorModeInstantaneous];
push.magnitude = 1; //Play with this, it's how much force is applied to your object
push.angle = 0; //play with this too
[self.animator addBehavior:push];
我从编译器中输入了这个 - 让我知道它是否有效。我们的想法是使用UIKitDynamics作为物理引擎,使用UICollisionBehavior让项目在框内反弹,并使用UIPushBehavior来应用初始力。
如果物品对你来说减速太快,或者当它从墙壁反弹时失去太多能量,你可以调整它的属性:
UIDynamicItemBehavior *behavior = [[UIDynamicItemBehavior alloc] initWithItems:@[self.itemThatBouncesAround]];
behavior.friction = 0; //no friction. play with this.
behavior.elasticity = 1;; //completely elastic, play with this.
[self.animator addBehavior:behavior];