iphone - 检查以编程方式生成的 ImageView 的相交

标签 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)) {

    }
}

当它们发生碰撞时,让图像做任何事情都没有运气。

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

最佳答案

如果您更改 View 的变换,它的框架不会改变(它甚至可能变得无效)并且应该被忽略。

来自documentation :

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

既然您所做的只是应用平移变换,为什么不为框架设置动画呢?

关于iphone - 检查以编程方式生成的 ImageView 的相交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12340327/

相关文章:

ios - 在 iOS 上获取 url 的缩影

ios - 进入调试器时出现 EXC_BAD_ACCESS(代码=1,地址=0x0)

swift - UIView 不调整大小以匹配设备

objective-c - 如何使用 NSWindowController 在标准应用程序中显示窗口?

iphone - 如何将 iphone 模拟器连接到 selgrid 2?

iphone - 删除分组 UITableViewCell 上重新排序控件旁边的白线

ios - Base64 图像编码 Swift 4 iOS

iphone - iOS 4 上的 iOS Storyboard?

iphone - SQLite - 从表中检索经纬度集

ios - 设置 UITableView 委托(delegate)和数据源时的最佳实践