【问题标题】:UIDynamicAnimator - won't work with UIView but will with Self.View?UIDynamicAnimator - 不能与 UIView 一起使用,但可以与 Self.View 一起使用?
【发布时间】:2014-03-20 16:19:05
【问题描述】:

我使用了 Apple 示例应用中的这段代码,并将其调整为我的。我希望 UIImageView gameOverFalling 运行此方法(跌倒和反弹)。它应该与 UIView finalScoreView 发生冲突,如您在第 2 行中所见。

-(void)gameOverFallingMethod{
    UIDynamicAnimator *animator = [[UIDynamicAnimator alloc] initWithReferenceView:finalScoreView];

    UIGravityBehavior *gravityBeahvior = [[UIGravityBehavior alloc] initWithItems:@[self.gameOverFalling]];
    [animator addBehavior:gravityBeahvior];

    UICollisionBehavior *collisionBehavior = [[UICollisionBehavior alloc] initWithItems:@[self.gameOverFalling]];
    // Apple said: "Creates collision boundaries from the bounds of the dynamic animator's
    // reference view (self.view)."
    collisionBehavior.translatesReferenceBoundsIntoBoundary = YES;
    collisionBehavior.collisionDelegate = self;
    [animator addBehavior:collisionBehavior];

    self.animator = animator;
}

但是,当我运行我的应用程序时,它会返回 Thread 1: SIGABRT 错误。在控制台中:

View item (<UIImageView: 0x10aaa0c70; frame = (15 175; 288 42);
  autoresize = RM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x10aa7e0a0>>)
should be a descendant of reference view in <UIDynamicAnimator: 0x10ae17610>
Stopped (0.000000s) in <UIView: 0x10aa9e1e0> {{0, 0}, {288, 195}}

当我将第 2 行的“finalScoreView”替换为“self.view”时,它可以工作,但它会落在整个屏幕的底部。

任何想法我做错了什么?我很想知道,谢谢!

【问题讨论】:

    标签: iphone animation view uiview


    【解决方案1】:

    您可能忘记使用以下方法将您的项目添加到参考视图:

    finalScoreView.addSubview(self.gameOverFalling)
    

    【讨论】:

      【解决方案2】:

      我通过在添加任何行为之前添加到子视图来解决这个问题。

      addSubview(square)
      var gravity = UIGravityBehavior(items: [square])
      gravity.magnitude = gravitySpeed
      animator.addBehavior(gravity)
      

      【讨论】:

        【解决方案3】:

        在您的视图层次结构中,self.gameOverFalling 不是从 finalScoreView 下降的例外情况。见UIDynamicAnimator class reference的引用:

        要为视图设置动画,请使用 initWithReferenceView 创建动画器: 方法。参考视图的坐标系作为 动画师的行为和项目的坐标系。每个动态 您与此类动画师关联的项目必须是 UIView 对象 并且必须从参考视图下降。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-11-14
          • 2012-07-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多