inVINCEable
inVINCEable

Reputation: 2206

Check Intersect for programmatically generated image views

i Have two images generated like this

- (void)moveImage:(UIImageView *)image duration:(NSTimeInterval)duration
            curve:(int)curve x:(CGFloat)x y:(CGFloat)y
{
    // Setup the animation
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:duration];
    [UIView setAnimationCurve:curve];
    [UIView setAnimationBeginsFromCurrentState:YES];

    // The transform matrix
    CGAffineTransform transform = CGAffineTransformMakeTranslation(x, y);
    image.transform = transform;

    // Commit the changes
    [UIView commitAnimations];

}
    - (void)alien {
    enemy =
    [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"alien.png"]];
    enemy.frame = CGRectMake(-100, 20, 50, 50);
    [self.view addSubview:enemy];

    // Move the image
    [self moveImage:enemy duration:5
              curve:UIViewAnimationCurveLinear x:460 y:0.0];  }
- (void)bullet4 {
    bullet =
    [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Bullet.png"]];
    bullet.frame = CGRectMake(carDude.center.x, 380, 5, 10);
    [self.view addSubview:bullet];

    // Move the image
    [self moveImage:bullet duration:3
              curve:UIViewAnimationCurveLinear x:0 y:-500.0];   }

I'm trying to hide them when they intersect. There can be many on the screen at the same time but i tried,

-(void)checkCollision {
    if(CGRectIntersectsRect(bullet.frame, enemy.frame)) {

    }
}

and no luck getting the images to do anything when they collide.

How can i get the image to hide and check collision?

Upvotes: 0

Views: 198

Answers (1)

DrummerB
DrummerB

Reputation: 40211

If you change the transform of a view, its frame doesn't change (it can even become unvalid) and should be ignored.

From the documentation:

Warning If this property is not the identity transform, the value of the frame property is undefined and therefore should be ignored.

Since all you're doing is applying a translation transform, why don't you animate the frame instead?

Upvotes: 2

Related Questions