【问题标题】:UIKit Dynamics UICollisionBehavior collision without bounceUIKit Dynamics UICollisionBehavior 无反弹的碰撞
【发布时间】:2013-11-18 02:04:34
【问题描述】:

我有一个视图,其边界设置为碰撞 (setTranslatesReferenceBoundsIntoBoundaryWithInsets) 和一个带有重力的子视图设置,以便它可以与超级视图边界发生碰撞。

我正在尝试使碰撞的弹性为 0%,但我还没有弄清楚怎么做。我为子视图尝试了UIDynamicItemBehavior,弹性为0,摩擦力也高得离谱,什么也没有。我的理由是,0 弹性已经意味着 0 力在冲击时再生,但即使是负数似乎也无济于事。

关于如何使碰撞吸收所有能量或使子视图在碰撞边界时不反弹的任何想法?

【问题讨论】:

    标签: uikit-dynamics uidynamicbehavior uicollisionbehavior


    【解决方案1】:

    我可能做错了,但以下似乎在一个简短的例子中起作用:

    为有问题的项目分配 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;
    }
    

    在那一刻将阻力设置为高值似乎可以缓解项目的反弹。

    【讨论】:

    • 好主意。效果很好,应该是公认的答案。
    【解决方案2】:

    需要设置弹性值:

    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];
    

    【讨论】:

      【解决方案3】:

      我知道怎么做,但还没有测试过。警告购买者!

      在您的视图上使用 UIDynamicItemBehavior。使用 UICollisionBehaviorDelegate 方法 collisionBehavior:endedContactForItem:withItem 在碰撞后将 resistance 设置为较高的值。 “阻力”就像物品和拥有者视图之间的摩擦。这是一个草图......

      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];
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多