我正在使用 UIView
动画在屏幕上移动一个按钮。当它移动时,我有一个 NSTimer
运行方法
checkForCollision
看起来像这样:
for(UIButton* b in squares)
if(CGRectIntersectsRect(playerSquare.frame, b.frame)) {
[self showEndMenu];
break;
}
间隔为 0.05;我希望该方法在按钮移过另一个按钮时随时通知我。问题似乎在于,当它检查 UIButton b
的框架时,它只会看到按钮正在向其移动的框架。
我有这样的动画按钮:
[UIView beginAnimations:@"" context:nil];
[UIView setAnimationDuration:1.3];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
view.center = destPoint;
[UIView commitAnimations];
因此,如果 destPoint 与 CGRect
playerSquare.frame
相交,则调用 [self showEndMenu]
。但是,如果按钮所在的帧/位置,比方说,在动画进行到一半时,与 playerSquare
相交,则不会调用 [self showEndMenu]
。我不知道如何解决这个问题;如果需要,我可以提供更多代码。谢谢!
最佳答案
如您所见,通过动画化 View 的属性来移动 View 会立即将属性设置为最终值。
您 _may_be 能够使用核心动画层获取中间值(请参阅 http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Animation_Types_Timing/Articles/PropertyAnimations.html#//apple_ref/doc/uid/TP40006672-SW1 在手机上打字,抱歉链接不畅)但老实说我对此没有任何经验。如果有人这样做,很高兴在这里看到它。事实上,快速搜索已经找到了这个问题,我认为有你正在寻找的确切答案:)
UIView animation determine center during animation
您的另一个选择是使用计时器反复移动按钮,就像使用 cocos2d 移动对象一样,所以当您每次检查位置时,您的按钮就在它们真正所在的位置。然而,这种方式你必须自己计算路径。
关于ios - 使用 UIView 动画时的 UIButton.frame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8074931/