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