问题描述
这是我的问题:我有两个图像:flakeImage 和 ViewToRotate.我想要的是,如果 flakeImage 触及 ViewToRotate,ViewToRotate.alpha=0.5;但是当 FlakeImage 出现在屏幕上时 ViewToRotate.alpha=0.5;不碰它.我认为这是我的观点有问题,因为我有:
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];
这里是代码:
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.
推荐答案
CGRect 有交集功能.UIViews 的框架是 CGRects.
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.
圆形碰撞:
a^2 + b^2
c^2 表示它们碰撞
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
这篇关于两个图像碰撞的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!