我正在为自定义视图运行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];
}