我应该删除行为UIPushBehavior - 我正在添加许多推送 [英] Should I removeBehavior a UIPushBehavior -- I am adding many pushes

查看:139
本文介绍了我应该删除行为UIPushBehavior - 我正在添加许多推送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小的UIView在一个盒子里弹跳。我正在添加瞬时模式。推。实际上我正在添加很多推 - 5或更多Hz。



神秘:



(1)我是否必须 removeBehavior ???? 如果是这样.. 何时?!? 之后是一个瞬间?



(2)是否UIPushBehaviorModeInstantaneous是一个特例,你不必(或不能)删除那些?? / p>

(3)当你添加行为时:那会......保留吗?! UIPushBehavior?要么?? WTF?!



(4)我似乎无法在任何地方找到这些方面!

   - (void)pushMeRight:(CGFloat)mag 
{
if(self.bounceAnimator == nil)return;
NSLog(@........... push me right%。2f,mag);

UIPushBehavior * pushRight = [[UIPushBehavior alloc]
initWithItems:@ [self]
mode:UIPushBehaviorModeInstantaneous];

pushRight.magnitude = mag;
pushRight.angle = radians(0); //(注意,0是对的,90是向下的)
[self.bounceAnimator addBehavior:pushRight];
}

{注意:我每次只分配UIPushBehavior 我需要一个。请注意,如果您尝试将只使用一个作为属性,则无效。事实上,Rob在下面解释了原因。}






解决方案



经过极为广泛的测试后,我们发现使用.action的Rob的第二解决方案基本上是完美的。



经过大量测试后再次,我们强烈建议以下代码,实际上,解决方案,编码重复推送的唯一方法..谢谢执行Rob:/

   - (void)pushAllUp:(CGFloat)mag 
{
if(self.bounceAnimator == nil)return;
for(UIView * pushme in self.subviews)
{
UIPushBehavior * pp =
[[UIPushBehavior alloc] initWithItems:@ [pushme]
mode:UIPushBehaviorModeInstantaneous] ;

pp.magnitude = mag;
pp.angle = radians(270); //(注意,0表示正确,90表示已关闭)

UIPushBehavior __weak * weakPP = pp;
pp.action = ^ {if(!weakPP.active)
[self.bounceAnimator removeBehavior:weakPP];};

[self.bounceAnimator addBehavior:pp];
}
}


解决方案

In回答你的问题:


  1. 是的,你要添加 UIPushBehavior 正确。


  2. 从技术上讲,您不必为瞬时推送调用 removeBehavior ,因为行为将一旦发生瞬时推送,其活动状态就会立即关闭。



    话虽如此,我倾向于删除这种行为,因为你正在占用记忆,动画师保持对这些非<$的强烈引用c $ c>有效即时推送行为。 (通过记录动画师的行为属性可以很容易地验证这一点,该属性是它正在跟踪的所有行为的数组。)最终可能存在与性能相关的问题它也会迭代所有这些非活跃的行为,但我怀疑内存问题可能更为重要。



    但与其他行为(例如 UISnapBehavior )保持有效不同,你不必担心关于延迟的瞬时推送行为继续影响添加它的项目。


  3. 它们没有过期,本身,但是,是的,他们很快进入活跃的状态


  4. 是的,当您向动画师添加行为时,动画师将保留对它的强引用,直到您删除该行为。


就个人而言,我倾向于在添加后删除该行为。因为它是瞬间的,它的移除时间并不是非常关键,你可以做一些简单的事情:

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

或者,您可以设置操作为你删除它。

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

UIPushBehavior __weak * weakPush = push; //避免强引用周期
push.action = ^ {
if(!weakPush.active){
[self.animator removeBehavior:weakPush];
}
};

[self.animator addBehavior:push];


I have a small UIView that bounces in a box. I am adding an Instantaneous mode. push. In fact I am adding very many pushes -- 5 or more Hz.

Mystery:

(1) Do I have to removeBehavior???? if so .. when?!? "after" an "instant" ?

(2) Is it that UIPushBehaviorModeInstantaneous is a special case, and you do not have to (or cannot) remove those??

(3) When you addBehavior: does that .. retain?! the UIPushBehavior? Or?? WTF?!

(4) I couldn't seem to find these aspects documented anywhere!

-(void)pushMeRight:(CGFloat)mag
    {
    if ( self.bounceAnimator == nil ) return;
    NSLog(@"...........push me right %.2f", mag);

    UIPushBehavior *pushRight = [[UIPushBehavior alloc]
            initWithItems:@[self]
            mode:UIPushBehaviorModeInstantaneous];

    pushRight.magnitude = mag;
    pushRight.angle = radians(0);   // (NB, 0 is right, 90 is down)
    [self.bounceAnimator addBehavior:pushRight];
    }

{Note: I simply alloc the UIPushBehavior each time I need one. Note that if you try to use "just one" as a property, it does not work. In fact, Rob explains why below.}


Solution

After extremely extensive testing, we found that Rob's "second" solution, using the .action, is essentially perfect.

Again after vast testing, we would very strongly recommend that the following code, is, really, "the solution", the only way to code repetitive pushes.. Thank goodness for Rob :/

-(void)pushAllUp:(CGFloat)mag
    {
    if ( self.bounceAnimator == nil ) return;
    for ( UIView *pushme in self.subviews )
        {
        UIPushBehavior *pp =
        [[UIPushBehavior alloc]initWithItems:@[ pushme ]
             mode:UIPushBehaviorModeInstantaneous];

        pp.magnitude = mag;
        pp.angle = radians(270); // (NB, 0 is right, 90 is down)

        UIPushBehavior __weak *weakPP = pp;
        pp.action = ^{ if (!weakPP.active)
             [self.bounceAnimator removeBehavior:weakPP];};

        [self.bounceAnimator addBehavior:pp];
        }
    }

解决方案

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];

这篇关于我应该删除行为UIPushBehavior - 我正在添加许多推送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆