【发布时间】:2014-09-03 14:23:48
【问题描述】:
我有一个小 UIView,它在一个盒子里弹跳。我正在添加 Instantaneous 模式。推。事实上,我添加了很多推动力——5 赫兹或更多赫兹。
谜底:
(1) 如果需要,我是否必须 removeBehavior?? .. 何时?!? “之后”是“瞬间”?
(2) 是不是 UIPushBehaviorModeInstantaneous 是一个特例,你不必(或不能)删除那些??
(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); // (NB, 0 is right, 90 is down)
[self.bounceAnimator addBehavior:pushRight];
}
{注意:我只是简单地分配 UIPushBehavior 每次我需要一个。请注意,如果您尝试使用“just one”作为属性,它不起作用。事实上,Rob 在下面解释了原因。}
解决方案
经过极其广泛的测试,我们发现 Rob 使用 .action 的“第二种”解决方案基本上是完美的。
再次经过大量测试,我们强烈建议使用以下代码,真的,“解决方案”,是编写重复推送的唯一方法。感谢 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];
}
}
【问题讨论】:
标签: ios uikit-dynamics uidynamicbehavior