使用 UIView 动画时的 UIButton.frame
我正在使用 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]
不会被调用。我不知道如何解决这个问题;如果需要,我可以提供更多代码。谢谢你!
I am using a UIView
animation to move a button across the screen. While it is moving, I have an NSTimer
running the method
checkForCollision
which looks like this:
for(UIButton* b in squares)
if(CGRectIntersectsRect(playerSquare.frame, b.frame)) {
[self showEndMenu];
break;
}
with an interval of .05; I would like for that method to notify me any time the button moves across the path of another. The problem, it appears, is that when it checks for the frame of the UIButton b
, it only sees the frame towards which the button is moving.
I have the button being animated like this:
[UIView beginAnimations:@"" context:nil];
[UIView setAnimationDuration:1.3];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
view.center = destPoint;
[UIView commitAnimations];
So, if the destPoint intersects the CGRect
playerSquare.frame
, [self showEndMenu]
is called. But if the frame / position at which the button is, say, halfway through the animation, intersects playerSquare
, [self showEndMenu]
doesn't get called. I don't know how to solve this; I can provide more code if need be. Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如您所发现的,通过动画属性来移动视图会立即将属性设置为最终值。
您可能能够使用核心动画层获取中间值(请参阅http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Animation_Types_Timing/Articles/PropertyAnimations.html#//apple_ref/doc/uid/TP40006672-SW1 在手机上输入抱歉链接不良)但老实说我对此没有任何经验。如果有人这样做的话,很高兴能在这里看到它。事实上,快速搜索已经找到了这个问题,我认为您正在寻找确切的答案:)
UIView 动画在动画期间确定中心
您的另一个选择是使用计时器迭代地移动按钮,就像使用 cocos2d 移动对象一样,因此当您检查每个位置时时间到了,你的按钮就回到了它们真正的位置。然而,这样你就必须自己计算路径。
Moving views by animating their properties immediately sets the property to the final value, as you have found.
You _may_be able to get the intermediate values out using the core animation layers (see http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Animation_Types_Timing/Articles/PropertyAnimations.html#//apple_ref/doc/uid/TP40006672-SW1 typing on phone sorry for poor link) but honestly I've not got any experience with that. If anyone does it would be great to see it here. In fact, a quick search has turned up this question, with the exact answer you are looking for, I think :)
UIView animation determine center during animation
Your other option is to move the button iteratively using timers, in the same way that objects are moved around using cocos2d, so when you are checking for position each time then your buttons are where they really are. However this way you have to calculate the path yourself.