Problem with collision images, collisions before touching

Hi everyone, I'm French, so excuse me for my English. My problem is this: I have an image in the center of the screen called viewToRotate, then I have an image called flakeView that is created off-screen and then it moves to the center, and every second the timer does this (create a flakeView off-screen, and then move it to the center of the screen).

What I wanted to do was: if flakeView and viewToRotate collide, reduce the view alpha of viewToRotate to 0.5. But when flakeView appears on the screen, the alpha reduction action is called without collision viewToRotate and flakeView, so they collide before they touch. I do not know why. How can i solve this. Here is the code:

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;
                 }];
}


-(void)checkCollision{

if(CGRectIntersectsRect(flakeView.frame, viewToRotate.frame) == 1)  
{
    viewToRotate.alpha=0.5;
}
}
+3
3

viewDidLoad. , - . viewDidLoad - .

- UIView. 60 , . . , . . , , , .

, - UIView ( ) . OpenGL.

0

, , , viewToRotate.frame , (. ).

, presentationLayer , .

UIView.layer.presentationLayer
, .

, :

-(void)checkCollision
{
    if(CGRectIntersectsRect(flakeView.layer.presentationLayer.frame, viewToRotate.layer.presentationLayer.frame) == 1)  
    {
        viewToRotate.alpha=0.5;
    }
}

CoreAnimation Layer hitTest: .

hitTest:
( ) .

0

These are the following things you would like to check.

  • Do not start with an image, use init with a frame, and then do flakeView.image = ----; Because your image can be quite wide initially and, therefore, collide from the moment of initialization.
  • Try alternatives for .center, e.g. .frame.size.x, because I had problems with .center as well
  • You could probably NSLog limit both ur images before and after alpha reduction
0
source

All Articles