0

我正在为自定义视图运行 UIGravityBehavior 和 UICollisionBehavior。我希望在 2-3 秒后触发 UISnapBehavior。这是我的代码:

-(void) applyDynamics
{
    _animator = [[UIDynamicAnimator alloc] initWithReferenceView:_referenceView];
    _gravity = [[UIGravityBehavior alloc] initWithItems:@[self]];
    _collision = [[UICollisionBehavior alloc] initWithItems:@[self]];
    _itemBehavior = [[UIDynamicItemBehavior alloc] initWithItems:@[self]];

    [_gravity setGravityDirection:CGVectorMake(0, 1.2)];

    _itemBehavior.elasticity = 0.8f;

    [_collision addBoundaryWithIdentifier:@"AZNotificationBoundary" fromPoint:CGPointMake(0, self.bounds.size.height) toPoint:CGPointMake(_referenceView.bounds.size.width, self.bounds.size.height)];

    [_animator addBehavior:_gravity];
    [_animator addBehavior:_collision];
    [_animator addBehavior:_itemBehavior];

    [self performSelector:@selector(hideAZNotification) withObject:nil afterDelay:2.0];

}

-(void) hideAZNotification
{
    dispatch_async(dispatch_get_main_queue(), ^{
        snap = [[UISnapBehavior alloc] initWithItem:self snapToPoint:CGPointMake(_referenceView.center.x, -10)];

        [_animator addBehavior:snap];
    });
}

我正在使用 [self performSelector] 调用 hideAZNotification 方法。该方法被调用,但从不附加快照行为。

更新:

奇怪的是,重力行为很好:

-(void) hideNotification
{
    // remove the original gravity behavior
    [_animator removeBehavior:_gravity];

    _gravity = [[UIGravityBehavior alloc] initWithItems:@[self]];
    [_gravity setGravityDirection:CGVectorMake(0, -1)];

    [_animator addBehavior:_gravity];
}
4

0 回答 0