Search code examples
iosobjective-ccgrect

CGRectContainsPoint doesn't return YES but on simulator the point is in the image


In viewDidLoad I am making an imageView called DeepFryingBasket. You can move the basket with your finger left and right but not up or down. The second image I have is one french frie that is falling down the screen. If the frie and the basket collide, the frie should disappear so it looks like it falls in the basket. Thats why I have an NSTimer that fires every 0.01 sec the CollisionDetection method.

The problem is that when I catch the french frie with my basket the frie doesn't disappear. I am sure the collisionDetection method is called but the compiler never gets in the if-statement and I don't know why. Any help would be really appreciated!

- (void)viewDidLoad
{
    [super viewDidLoad];
    DeepFryingBasket =[[UIImageView alloc] initWithFrame:CGRectMake(110,468,100,80)];
    DeepFryingBasket.image = [UIImage imageNamed:@"DeepFryingBasket"];
    [self.view addSubview:DeepFryingBasket];
    CollisionDetection = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(collisionDetection) userInfo:nil repeats:YES];
    [CollisionDetection fire];
}

-(void)collisionDetection{
    if (CGRectContainsPoint (FrenchFrie.frame, DeepFryingBasket.center)){
        [FrenchFrie removeFromSuperview];
        points = points + 1;
        dropped = dropped +1;
    }
}

I move the basket with the following code:

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [[event allTouches] anyObject];
    touchLocation = [touch locationInView:self.view];
    frame = DeepFryingBasket.frame;
    if (CGRectContainsPoint(frame, touchLocation)){
        DeepFryingBasket.center = CGPointMake(touchLocation.x, DeepFryingBasket.center.y);
    }
}

EDIT: I also tried adding

    DeepFryingBasket.userInteractionEnabled = YES;

but that didn't change much. I also found out that while the french frie falls with an animation, the origin of the image didn't change. Why isn't this changing? I noticed that when I move the basket it's center.x moves along with my finger. I move the French frie with the following code:

-(void)letFrenchFrieFall {
    [UIView animateWithDuration:10.0f delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
    FrenchFrie.frame = CGRectMake(randomX, (int)[[UIScreen mainScreen] bounds].size.height + 40, FrenchFrie.frame.size.width, FrenchFrie.frame.size.height);
    } completion:^(BOOL finished){
        [FrenchFrie removeFromSuperview];
    }];
}

Solution

  • OK so I found the answer to my own question: while an imageView is moving due to an animation, the x- and Y-coordinate are set to the place where the animation ends; for this reason it is impossible to get it's location with

    FrenchFrie.frame
    

    You can use

    FrenchFrieFrame = [[FrenchFrie.layer presentationLayer] frame];
    

    to get the frame that is currently displayed. Don't forget to include the Quartzcore Framework and import it.

    #import <QuartzCore/QuartzCore.h>