I am running UIGravityBehavior and UICollisionBehavior for a custom view. I want that after 2-3 seconds a UISnapBehavior is triggered. Here is my code:
-(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];
});
}
I am using [self performSelector] to invoke the hideAZNotification method. The method is invoked but the snap behavior is never attached.
UPDATE:
Weirdly enough the gravity behavior works fine:
-(void) hideNotification
{
// remove the original gravity behavior
[_animator removeBehavior:_gravity];
_gravity = [[UIGravityBehavior alloc] initWithItems:@[self]];
[_gravity setGravityDirection:CGVectorMake(0, -1)];
[_animator addBehavior:_gravity];
}