两幅图像碰撞的问题
嗯,这是我的问题: 我有两个图像: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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我对此有一些二年级的经验,写过 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.
CGRect有一个交集函数。 UIView 的框架是 CGRect。
我预见的问题是,如果矩形有很多空白,它们在实际接触之前就会出现相交
其次,您应该切换到块动画。强烈鼓励
这一切都是凭记忆做的,不知道是否有错误。
圆形碰撞:
a^2 + b^2 < c^2 表示它们发生碰撞
再次,全部凭记忆,自行检查是否有错误
CGRect has a intersection function. The frames of UIViews are CGRects.
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
Did this all from memory, no idea if there are errors.
Circular Collision:
a^2 + b^2 < c^2 means they collide
Again, all from memory, check for errors on your own