两幅图像碰撞问题

发布于 2024-11-17 06:59:06 字数 562 浏览 4 评论 0原文

这是我的代码:

-(void)collision {
    if(CGRectIntersectsRect(ball.frame,center.frame)) {
        center.alpha=0.1;
    }
}

-(void)viewDidLoad {
    [super viewDidLoad];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:7.0f];
    [ball setCenter:CGPointMake(200, 100)];
    [UIView commitAnimations];
    [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(collision) userInfo:nil repeats:YES];
}

我的问题是,当viewDidLoad“center.alpha=0.1”但“center”和“ball”尚未碰撞时,我不知道为什么,我认为这是由于动画。

Here is my code :

-(void)collision {
    if(CGRectIntersectsRect(ball.frame,center.frame)) {
        center.alpha=0.1;
    }
}

-(void)viewDidLoad {
    [super viewDidLoad];
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:7.0f];
    [ball setCenter:CGPointMake(200, 100)];
    [UIView commitAnimations];
    [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(collision) userInfo:nil repeats:YES];
}

My problem is that when viewDidLoad "center.alpha=0.1" but "center" and "ball" have not collided yet , I don't know why, I think it is due to the animation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

只为一人 2024-11-24 06:59:06

尽管动画需要 7 秒,但 [ball setCenter:CGPointMake(200, 100)]; 会立即设置,因此 - (void)collision 可能会将您的 alpha 设置为动画中“球”与“中心”相交之前的 0.1。

您可以使用 NSTimer 来慢慢改变“球”的中心,而不是 UIView 动画。

Although the animation takes 7 seconds, [ball setCenter:CGPointMake(200, 100)];is set immediately and because of that - (void)collision probably sets your alpha to 0.1 before "ball" intersects with "center" in the animation.

Instead of UIView animations you could use a NSTimer to slowly change the center of "ball".

浮世清欢 2024-11-24 06:59:06

您计划在 viewDidLoad 结束时执行该行 0.01 秒后调用碰撞。但视图尚未显示,因此显示视图可能需要超过 0.01 秒的时间。

尝试 viewDidAppear

话虽如此,我认为您还不清楚 iOS 中动画的用途。这些不是用于计算碰撞检测 - 它们只是用于在设定的时间内将物体从起点移动到终点。我建议你阅读有关动画系统的 Apple 文档。

You are scheduling the call to collision 0.01 seconds after the line is executed at the end of viewDidLoad. But the view hasn't been displayed yet and so it could take longer than 0.01 seconds to display the view.

Try viewDidAppear

Having said that, I think you are not clear on the purpose of the animtions in iOS. These are not for calculating collision detection - they are just for moving things from a start point to an end point over a set time. I would suggest you read the Apple docs on the animation system.

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