检查Intersect以获取以编程方式生成的图像视图

时间:2012-09-09 15:21:25

标签: iphone ios xcode ios5.1

我有两张像这样生成的图片

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

我试图在它们相交时隐藏它们。屏幕上可以有很多同时但我试过了,

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

    }
}

并且没有运气让图像在碰撞时做任何事情。

如何让图像隐藏并检查碰撞?

1 个答案:

答案 0 :(得分:2)

如果更改视图的变换,其框架不会更改(甚至可能无效),应忽略。

来自documentation

  

警告如果此属性不是标识转换,则frame属性的值未定义,因此应忽略。

由于您所做的只是应用转换变换,为什么不对帧进行动画处理?

相关问题