我有一个视图,其边界设置为碰撞(setTranslatesReferenceBoundsIntoBoundaryWithInsets
)和子视图设置与重力,以便它可以碰撞超视图边界。
我试图使碰撞0%有弹性,但我还没弄明白怎么回事。我为子视图尝试了一个UIDynamicItemBehavior
,弹性为0,也有非常高的摩擦力。我的理由是,0弹性已经意味着在力的作用下再次出现力量再生,但即使是负数似乎也没有做任何事情或者说很少。
关于如何进行碰撞的任何想法都会吸收所有的能量或任何使得子视图在碰撞边界时不会弹跳所需的能量?
答案 0 :(得分:3)
我可能做错了,但以下似乎只是一个简短的例子:
为相关项目分配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;
}
此时将阻力设定为高值似乎可以减轻其反弹的程度。
答案 1 :(得分:1)
你需要设置弹性值:
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];
答案 2 :(得分:0)
我知道该怎么做,但还没有测试过。注意事项!
在视图上使用UIDynamicItemBehavior。碰撞后将阻力设置为较高的值,使用UICollisionBehaviorDelegate方法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];
}