Ios 在延迟后附加一个行为
我正在为自定义视图运行UIGravityBehavior和UICollisionBehavior。我希望在2-3秒后触发UISnapBehavior。这是我的密码:Ios 在延迟后附加一个行为,ios,uikit-dynamics,Ios,Uikit Dynamics,我正在为自定义视图运行UIGravityBehavior和UICollisionBehavior。我希望在2-3秒后触发UISnapBehavior。这是我的密码: -(void) applyDynamics { _animator = [[UIDynamicAnimator alloc] initWithReferenceView:_referenceView]; _gravity = [[UIGravityBehavior alloc] initWithItems:@[sel
-(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];
}
添加一些登录
hideAZNotification
,以确保一切正常。\u animator
和\u referenceView
是否正常?self
仍然是\u referenceView
的子视图吗?我不明白为什么他们不会,但这总是值得肯定的。我更新了代码。重力作用很好,等等。hideAZNotification
是否与hideAZNotification
有所不同?这到底是怎么回事?请你把所有代码都显示出来好吗?对不起!我刚刚将hideAZNotification重命名为hideNotification。你做了我要求的日志记录吗?在创建快照之后,您是否尝试记录它本身?如果为nil,则创建它时出现问题,这可能就是问题所在。