CGRect 在动画过程中不改变

发布于 2024-11-29 08:11:14 字数 1237 浏览 1 评论 0原文

我正在对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

山川志 2024-12-06 08:11:14

UIView 几何更新立即应用于其 CALayer,即使在动画块中也是如此。要获取应用了动画的图层版本,您可以使用 -[CALayerpresentationLayer],如下所示(警告 - 未经测试的代码):

- (void) checkCollision {
    CGRect oncomerFrame = oncomer1.layer.presentationLayer.frame;
    bool collision = CGRectIntersectsRect(car.frame, oncomerFrame);
    if (collision) {
        NSLog(@"BOOOOOOOM");
    } else {
        NSLog(@"Oncomer: %@", NSStringFromCGRect(oncomerFrame));
    }

}

UIView geometry updates apply immediately to their CALayer, 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):

- (void) checkCollision {
    CGRect oncomerFrame = oncomer1.layer.presentationLayer.frame;
    bool collision = CGRectIntersectsRect(car.frame, oncomerFrame);
    if (collision) {
        NSLog(@"BOOOOOOOM");
    } else {
        NSLog(@"Oncomer: %@", NSStringFromCGRect(oncomerFrame));
    }

}
北笙凉宸 2024-12-06 08:11:14

从您的代码来看:

CGRect f = oncomer1.frame;
f.origin.y = self.view.frame.size.height+oncomer1.frame.size.height;
oncomer1.frame = f;

框架永不改变的逻辑解释是,您仅更改框架的 y,并且始终将其设置为由两个 heights.

From your code:

CGRect f = oncomer1.frame;
f.origin.y = self.view.frame.size.height+oncomer1.frame.size.height;
oncomer1.frame = f;

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 two heights.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文