Should I removeBehavior a UIPushBehavior — I am adding many pushes

你离开我真会死。 提交于 2019-12-02 02:03:51

In answer to your questions:

  1. Yes, you are adding that UIPushBehavior correctly.

  2. You technically don't have to call removeBehavior for an instantaneous push because the behavior will have its active status turned off as soon as the instantaneous push takes place.

    Having said that, I'd be inclined to remove the behavior because you're otherwise taking up memory, with the animator maintaining a strong reference to these non-active instantaneous push behaviors. (This is easily verified by logging the animator's behaviors property, which is an array of all of the behaviors it is keeping track of.) There might eventually be a performance related issue for it to iterate through all of these non-active behaviors, too, though I suspect the memory concerns are probably of more significant interest.

    But unlike other behaviors (e.g. UISnapBehavior) which stay active, you don't have to worry about lingering instantaneous push behaviors continuing to affect the items to which it was added.

  3. They don't "expire", per se, but, yes, they quickly go to an active state of NO.

  4. Yes, when you add a behavior to the animator, the animator will maintain a strong reference to it until you remove the behavior.

Personally, I'd be inclined to remove the behavior after adding it. Because it's instantaneous, the timing of its removal isn't terribly critical, and you could do something as simple as:

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.5 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
    [self.animator removeBehavior:push];
});

Or, you could set up an action that removed it for you.

UIPushBehavior *push = [[UIPushBehavior alloc] initWithItems:self.items mode:UIPushBehaviorModeInstantaneous];
push.pushDirection = ...

UIPushBehavior __weak *weakPush = push;  // avoid strong reference cycle
push.action = ^{
    if (!weakPush.active) {
        [self.animator removeBehavior:weakPush];
    }
};

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