判断两个区域是否重叠


//CGRectContainsRect(<#CGRect rect1#>, <#CGRect rect2#>)判断两个区域是否重叠

判断给定的点是否被一个CGRect包含,可以用CGRectContainsPoint函数

BOOL contains = CGRectContainsPoint(CGRect rect, CGPoint point);

判断一个CGRect是否包含再另一个CGRect里面,常用与测试给定的对象之间是否又重叠

BOOL contains = CGRectContainsRect(CGRect rect1, CGRect rect2);

判断两个结构体是否有交错.可以用CGRectIntersectsRect

BOOL contains = CGRectIntersectsRect(CGRect rect1, CGRect rect2);

float float_ = CGRectGetMaxX(CGRect rect);返回矩形右边缘的坐标

CGRectGetMaxY返回矩形顶部的坐标

CGRectGetMidX返回矩形中心X的坐标

CGRectGetMidY返回矩形中心Y的坐标

CGRectGetMinX返回矩形左边缘的坐标

CGRectGetMinY返回矩形底部的坐标

CGRectContainsPoint 看参数说明,一个点是否包含在矩形中,所以参数为一个点一个矩形


//爆炸图片数组创建

boomImageArray= [[NSMutable Arrayalloc]init];

for(inti = 0; i < 5 ; i++) {

UIImage* image = [UIImage imageNamed:[NSString stringWithFormat:@"bz%d",i+1]];

[boomImage ArrayaddObject:image];

}

for(UIImageView *_emenyPlane in emenyPlaneArray) {

//敌机与子弹的碰撞

for(UIImageView *_myBullet in myBulletArray) {

//CGRectContainsRect(<#CGRect rect1#>, <#CGRect rect2#>)判断两个区域是否重叠

if(CGRectContainsPoint(_emenyPlane.frame, _myBullet.center)){

UIImageView *boomImageView = [[UIImageView alloc] initWithFrame:_emenyPlane.frame];

[self.view addSubview:boomImageView];

boomImageView.alpha= 0.8;

boomImageView.animationDuration= 0.5;

boomImageView.animationImages=boomImageArray;

boomImageView.animationRepeatCount= 1;

[boomImageView startAnimating];

//_emenyPlane.frame = CGRectMake(0, -40, 40, 40);

_emenyPlane.tag= 100;

//_myBullet.frame = CGRectMake(_MyPlane1.frame.origin.x+25,-10, 5, 10);

//_myBullet.tag = 100;

//延迟时间去执行移除视图操作

[self performSelector:@selector(removeBoomView:) withObject:boomImageView afterDelay:0.5];

}

}

你可能感兴趣的:(判断两个区域是否重叠)