Is it possible to pause a UIDynamicAnimator without removing and adding UIDynamicBehaviours?

这一生的挚爱 提交于 2020-01-02 03:16:08

问题


I'm implementing some animations using UIDynamics to enable some physics-based transitions between views. I'd like to pause and continue these animations programatically in response to user touches. The only way I've found to do this so far is by removing the behaviours from the dynamic animator and then re-adding them like this:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.dynamicAnimator removeBehaviour: self.behaviour1];
    [self.dynamicAnimator removeBehaviour: self.behaviour2];
    // etc
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    self.behaviour1 = [[UIGravityBehavior alloc] initWithItems: @[self.item]];
    self.behaviour1.gravityDirection = self.gravityVector;
    [self.dynamicAnimator addBehaviour: self.behaviour1];

    self.behaviour2 = [[UICollisionBehavior alloc] initWithItems: @[self.item]];
    [self.behaviour2 addBoundaryWithIdentifier: @"Boundary" forPath: self.boundary];
    [self.dynamicAnimator addBehaviour: self.behaviour2];

    // etc
}

(Aside: I've also used [self.dynamicAnimator updateItemUsingCurrentState: item] instead of re-creating the behaviours, but I still have to remove them from the dynamic animator and re-add them again in order to pause whilst the user is touching the screen.)

What I'd like to be able to do is self.dynamicAnimator.running = NO to pause, and then self.dynamicAnimator.running = YES to continue. This would be much neater, involve less code (I currently have five behaviours to do this with) and would help avoid potential bugs in the process. Unfortunately, the dynamicAnimator.running property is readonly so it's impossible.

Is there a way of pausing and continuing the UIDynamicAnimator in a line or two that I've missed?

来源:https://stackoverflow.com/questions/21452151/is-it-possible-to-pause-a-uidynamicanimator-without-removing-and-adding-uidynami

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!