两幅图像碰撞的问题

发布于 2024-11-05 21:30:22 字数 1062 浏览 5 评论 0原文

嗯,这是我的问题: 我有两个图像:flakeImage 和 ViewToRotate。我想要的是,如果 flakeImage 触及 ViewToRotate,ViewToRotate.alpha=0.5;但是当FlakeImage出现在屏幕上时ViewToRotate.alpha=0.5;无需触摸它。我认为这是我的观点有问题,因为我有:

UIImageView* flakeView = [[UIImageView alloc] initWithImage:flakeImage];

这是代码:

UIImageView* flakeView = [[UIImageView alloc] initWithImage:flakeImage];

// use the random() function to randomize up our flake attributes
int startY = round(random() % 320);

// set the flake start position
flakeView.center = CGPointMake(490, startY);
flakeView.alpha = 1;

// put the flake in our main view
[self.view addSubview:flakeView];

[UIView beginAnimations:nil context:flakeView];
// set up how fast the flake will fall
[UIView setAnimationDuration:7 ];

// set the postion where flake will move to
flakeView.center = viewToRotate.center;

// set a stop callback so we can cleanup the flake when it reaches the
// end of its animation
[UIView setAnimationDelegate:self];
[UIView commitAnimations];

请问我该如何解决这个问题? 如果有人能帮助我那就太酷了。

Well here is my problem:
I have two images : flakeImage and ViewToRotate. What I want is that if flakeImage touches ViewToRotate, ViewToRotate.alpha=0.5; but when FlakeImage appears on the screen ViewToRotate.alpha=0.5; without touching it. I think it's a problem with my view beacause I have :

UIImageView* flakeView = [[UIImageView alloc] initWithImage:flakeImage];

here is the code :

UIImageView* flakeView = [[UIImageView alloc] initWithImage:flakeImage];

// use the random() function to randomize up our flake attributes
int startY = round(random() % 320);

// set the flake start position
flakeView.center = CGPointMake(490, startY);
flakeView.alpha = 1;

// put the flake in our main view
[self.view addSubview:flakeView];

[UIView beginAnimations:nil context:flakeView];
// set up how fast the flake will fall
[UIView setAnimationDuration:7 ];

// set the postion where flake will move to
flakeView.center = viewToRotate.center;

// set a stop callback so we can cleanup the flake when it reaches the
// end of its animation
[UIView setAnimationDelegate:self];
[UIView commitAnimations];

How can I solve this please ?
if someone could help me it would be very cool.

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

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

发布评论

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

评论(2

阳光①夏 2024-11-12 21:30:23

我对此有一些二年级的经验,写过 http:// itunes.apple.com/us/app/balls/id372269039?mt=8。如果您检查该应用程序,您会看到一些相同的问题。这个话题是一个相当深的兔子洞。当我启动该应用程序时,我什至不知道如何编写一个像样的游戏循环。您首先需要它,因为您需要进行精确的时间步长计算。 AFA 碰撞,您分别更新模型和视图,因此,如果您更新模型和对象重叠,则需要备份它们,直到它们不发生碰撞,然后用结果更新视图。如果您打算有很多碰撞对象,那么使用 UIView 可能会碰壁。让事情变得更复杂的是,如果您的对象是圆形的,则 CGRectIntersectsRect 将无法正常工作。这让数学有点复杂,但也不算太糟糕。在我的应用程序中,我发现很难让物理效果看起来更真实。问题是球 A 和 B 重叠,因此您将它们备份,然后它们现在与其他球相交,等等。 链接是一个很好的起点,但是有很多代码示例“几乎”可以工作。

I have a bit of sophomoric experience with this, having written http://itunes.apple.com/us/app/balls/id372269039?mt=8. If you check that app out, you will see a bit of the same problem. This topic is a pretty deep rabbit hole. WHen I started that app, I didn't even know how to write a decent game loop. You will need that first because you need to do precise time-step calculations. AFA the collisions, you update your model and view separately, so if you update the model and objects overlap, you need to back them up until they don't collide and then update your view with the result. If you plan to have a lot of colliding objects, you may hit a wall using UIViews. To complicate things more, if your objects are round, CGRectIntersectsRect won't exactly work. That complicates the math a bit, but it's not too bad. With my app, I found it quite difficult to get the physics to look realistic. THe problem became that ball A and B overlap, so you back them up, then they now intersect other balls, etc, etc. This link is a good starting point, but there are quite a few examples of code out there that "almost" work.

只是我以为 2024-11-12 21:30:23

CGRect有一个交集函数。 UIView 的框架是 CGRect。

if (CGRectIntersectsRect(view1.frame, view2.frame) == 1)
  NSLog(@"The views intersect");
else
  NSLog(@"The views do not intersect");

我预见的问题是,如果矩形有很多空白,它们在实际接触之前就会出现相交

其次,您应该切换到块动画。强烈鼓励

UIImageView* flakeView = [[[UIImageView alloc] initWithImage:flakeImage] autorelease];

// use the random() function to randomize up our flake attributes
int startY = round(random() % 320);

// set the flake start position
flakeView.center = CGPointMake(490, startY);
flakeView.alpha = 1;

// put the flake in our main view
[self.view addSubview:flakeView];

[UIView animateWithDuration:.7
                 animations:^ {
                         // set the postion where flake will move to
                         flakeView.center = viewToRotate.center;
                  };

这一切都是凭记忆做的,不知道是否有错误。

圆形碰撞:

a^2 + b^2 < c^2 表示它们发生碰撞

if(pow(view2.frame.origin.x - view1.frame.origin.x, 2) + 
    pow(view2.frame.origin.y - view1.frame.origin.y, 2) < 
    pow(view1.frame.size.width/2, 2))
{
     //collision
}
else
{
     //no collision
}

再次,全部凭记忆,自行检查是否有错误

CGRect has a intersection function. The frames of UIViews are CGRects.

if (CGRectIntersectsRect(view1.frame, view2.frame) == 1)
  NSLog(@"The views intersect");
else
  NSLog(@"The views do not intersect");

The problem I foresee is that if the rects have lots of whitespace, they will appear to intersect before they actually touch

Secondly, you should switch up to block animations. It's strongly encouraged

UIImageView* flakeView = [[[UIImageView alloc] initWithImage:flakeImage] autorelease];

// use the random() function to randomize up our flake attributes
int startY = round(random() % 320);

// set the flake start position
flakeView.center = CGPointMake(490, startY);
flakeView.alpha = 1;

// put the flake in our main view
[self.view addSubview:flakeView];

[UIView animateWithDuration:.7
                 animations:^ {
                         // set the postion where flake will move to
                         flakeView.center = viewToRotate.center;
                  };

Did this all from memory, no idea if there are errors.

Circular Collision:

a^2 + b^2 < c^2 means they collide

if(pow(view2.frame.origin.x - view1.frame.origin.x, 2) + 
    pow(view2.frame.origin.y - view1.frame.origin.y, 2) < 
    pow(view1.frame.size.width/2, 2))
{
     //collision
}
else
{
     //no collision
}

Again, all from memory, check for errors on your own

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