我有一个视图,它的边界被设置为碰撞(setTranslatesReferenceBoundsIntoBoundaryWithInsets)和一个带有重力的子视图设置,这样它就可以与superview边界发生冲突。
我正在尝试使碰撞有0%的弹性,但我还没有弄清楚怎么做。我尝试了子视图的UIDynamicItemBehavior,弹性为0,摩擦也高得离谱,什么也没有。我的理由是,弹性为0已经意味着碰撞时的强制回复为0,但即使是负数似乎也没有什么作用或作用很小。
关于如何让碰撞吸收所有能量,或者在碰撞边界时使子视图不反弹,你有什么想法吗?
发布于 2014-05-14 03:29:00
我可能做错了,但在一个简短的例子中,下面的方法似乎是有效的:
为有问题的项目分配UIDynamicItemBehavior:
self.itemBehaviorInQuestion = [[UIDynamicItemBehavior alloc] initWithItems:@[self.infoView]];
self.itemBehaviorInQuestion.resistance = 0;
self.collisionBehavior = [[UICollisionBehavior alloc] initWithItems:@[self.infoView]];
self.collisionBehavior.collisionDelegate = self;
[self.animator addBehavior:self.collisionBehavior];
[self.animator addBehavior:self.itemBehaviorInQuestion];实现以下UICollisionBehavior委托方法:
- (void)collisionBehavior:(UICollisionBehavior *)behavior beganContactForItem:(id<UIDynamicItem>)item withBoundaryIdentifier:(id<NSCopying>)identifier atPoint:(CGPoint)p
{
self.itemBehaviorInQuestion.resistance = 100;
}
- (void)collisionBehavior:(UICollisionBehavior *)behavior endedContactForItem:(id<UIDynamicItem>)item withBoundaryIdentifier:(id<NSCopying>)identifier
{
self.itemBehaviorInQuestion.resistance = 0;
}此时将阻力设置为较高的值似乎可以缓解该物品的反弹。
发布于 2014-03-14 11:54:48
您需要设置弹性的值:
UIView* square = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
self.view addSubview:square
UIDynamicAnimator* a = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
UIDynamicItemBehavior* behavior = [[UIDynamicItemBehavior alloc] initWithItems:@[square]];
behavior.elasticity = 0.5;
[a addBehavior:behavior];发布于 2013-11-18 13:51:31
我知道怎么做,但还没有测试过。买者自负!
在视图上使用UIDynamicItemBehavior。在冲突后,使用collisionBehavior:endedContactForItem:withItem.方法将阻力设置为较高的值“阻力”就像物品和拥有者之间的摩擦。这是一张素描。
UIDynamicItemBehavior *lowFriction = [[UIDynamicItemBehavior alloc] init];
lowFriction.resistance = 0.0;
UIDynamicItemBehavior *highFriction = [[UIDynamicItemBehavior alloc] init];
highFriction.resistance = 10.0;
yourCollidingView *ycv = [[yourCollidingView alloc] init]; // yourCollidingView is a subclass of UIView
[noFriction addItem:ycv];
[myAnimator addBehavior:lowFriction];
[myAnimator addBehavior:highFriction];然后在委托中
- (void)collisionBehavior:(UICollisionBehavior *)behavior endedContactForItem:(id<UIDynamicItem>)item1 withItem:(id<UIDynamicItem>)item2 {
[lowFriction removeItem:item1];
[lowFriction removeItem:item2];
[highFriction addItem:item1];
[highFriction addItem:item2];
}https://stackoverflow.com/questions/20038877
复制相似问题