0

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?

1
  • It animated the image across the view, ill update my question Sep 9, 2012 at 15:39

1 Answer 1

2

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?

2
  • I'm new to animating frames as you could probably tell by the mistake, that's the only way i know how to animate. could you point me int the direction of some code Sep 9, 2012 at 16:51
  • You do the same thing as you did, but instead of changing the transform, you change the frame or the center. image.center = CGPointMake(x, y);.
    – DrummerB
    Sep 9, 2012 at 21:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.