本文介绍了两个图像的碰撞问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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的框架是CGRect。

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");

我预见的问题是,如果rects有很多空格,实际上触摸

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

这篇关于两个图像的碰撞问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 17:38