【问题标题】:How to remove UIView after UIGravityBehavior has moved it off screen?UIGravityBehavior 将其移出屏幕后如何删除 UIView?
【发布时间】:2015-04-07 23:53:47
【问题描述】:

我试图在 action 属性上设置一个块,但这没有用......有什么想法吗?我知道 UIView 动画...方法有一个完成块,但不确定 UIDynamicAnimations。

编辑:添加代码

[self.animator removeAllBehaviors];
UIGravityBehavior *gravityBehaviour = [[UIGravityBehavior alloc] initWithItems:@[self.onscreen]];
gravityBehaviour.gravityDirection = CGVectorMake(0, 10);
gravityBehaviour.action = ^{
    if(self.onscreen.frame.origin.y > [UIScreen mainScreen].bounds.size.height)
       [self.onscreen removeFromSuperview];
    NSLog(@"locations is %f, height is %f", self.onscreen.frame.origin.y, [UIScreen mainScreen].bounds.size.height);
};
[self.animator addBehavior:gravityBehaviour];

UIDynamicItemBehavior *itemBehaviour = [[UIDynamicItemBehavior alloc] initWithItems:@[self.onscreen]];
[itemBehaviour addAngularVelocity:-M_PI_2 forItem:self.onscreen];

[self.animator addBehavior:itemBehaviour];

输出反映了即使在屏幕外,视图仍会继续移动。

【问题讨论】:

  • 在行为的 action 属性上设置一个块是正确的做法。请在此处发布您的代码。

标签: ios objective-c uiview core-animation uiviewanimation


【解决方案1】:

所以我通过在 if 语句中再添加一件事来解决它:[animator removeAllBehaviors];这似乎可以解决问题。

【讨论】:

【解决方案2】:
let dynamicBehaviour = UIDynamicItemBehavior(items: [randomWord])
weak var weakBehaviour = dynamicBehaviour
weak var weakSelf = self


dynamicBehaviour.action = {

  if let currentY = weakBehaviour?.linearVelocityForItem(rndWord).y {

    if currentY > CGRectGetMaxY(self.view.frame) / 2 {
      weakSelf?.animator?.removeAllBehaviors()
      weakSelf?.randomWord?.removeFromSuperview()
    }
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-24
    • 1970-01-01
    • 2022-12-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多