CGRect 在动画过程中不改变
我正在对 UIView 进行动画处理,并想检查它的框架是否与另一个 UIView 的框架相交。这就是我“生成”UIView 之一的方式:
- (void) spawnOncomer
{
oncomer1 = [[Oncomer alloc] initWithType:@"car"];
[self.view addSubview:oncomer1];
//make the oncomer race across the screen
[UIView beginAnimations:nil context:NULL]; {
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:3.0];
[UIView setAnimationDelegate:self];
CGRect f = oncomer1.frame;
f.origin.y = self.view.frame.size.height+oncomer1.frame.size.height;
oncomer1.frame = f;
[UIView setAnimationDidStopSelector:@selector(decountCar)];
}
[UIView commitAnimations];
}
到目前为止一切顺利。现在我想通过这样做来检查这个 UIView 和我的其他 UIView 是否发生碰撞:
- (void) checkCollision {
bool collision = CGRectIntersectsRect(car.frame, oncomer1.frame);
if (collision) {
NSLog(@"BOOOOOOOM");
} else {
NSLog(@"Oncomer: %@", NSStringFromCGRect(oncomer1.frame));
}
}
但是,它们永远不会发生碰撞。虽然我看到 oncomer1 在屏幕上移动,但登录 oncomer1.frame 永远不会改变:它不断输出 Oncomer: {{50, 520}, {30, 60}}
(这是动画后值) 。
有谁知道这是为什么?
Ps 这两个方法都是通过 NSTimer 直接或间接调用的,因此都是在后台执行的
I'm animating an UIView and want to check if its frame intersects with another UIView's frame. This is how I "spawn" one of the UIViews:
- (void) spawnOncomer
{
oncomer1 = [[Oncomer alloc] initWithType:@"car"];
[self.view addSubview:oncomer1];
//make the oncomer race across the screen
[UIView beginAnimations:nil context:NULL]; {
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:3.0];
[UIView setAnimationDelegate:self];
CGRect f = oncomer1.frame;
f.origin.y = self.view.frame.size.height+oncomer1.frame.size.height;
oncomer1.frame = f;
[UIView setAnimationDidStopSelector:@selector(decountCar)];
}
[UIView commitAnimations];
}
So far so good. Now I want to check if this UIView and my other UIView collide by doing this:
- (void) checkCollision {
bool collision = CGRectIntersectsRect(car.frame, oncomer1.frame);
if (collision) {
NSLog(@"BOOOOOOOM");
} else {
NSLog(@"Oncomer: %@", NSStringFromCGRect(oncomer1.frame));
}
}
However, they never collide. Although I see oncomer1 moving across the screen, loggin oncomer1.frame never changes: it keeps outputting Oncomer: {{50, 520}, {30, 60}}
(which are the post-animation values).
Does anyone know why this is?
P.s. Both methods are called directly or indirectly with a NSTimer and are thus performed in the background
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
UIView
几何更新立即应用于其CALayer
,即使在动画块中也是如此。要获取应用了动画的图层版本,您可以使用-[CALayerpresentationLayer]
,如下所示(警告 - 未经测试的代码):UIView
geometry updates apply immediately to theirCALayer
, even in an animation block. To get a version of a layer with animations applied, you can use-[CALayer presentationLayer]
, like this (warning - untested code):从您的代码来看:
框架永不改变的逻辑解释是,您仅更改框架的
y
,并且始终将其设置为由两个heights.
From your code:
A logical explanation of the frame never changing is that you are only changing the
y
of the frame, and you are always setting it to the same value determined by twoheight
s.