判断两个区域是否重叠


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

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • UIView(控件) 功能一:界面显示1. 屏幕上显示的所有UI元素都叫做控件,也有人叫做视图、组件;按钮(UIB...
    翻这个墙阅读 672评论 0 0
  • IOS开发几何类方法总结CGGeometry.h文件是用C语言实现的一个封装了许多常用几何方法的文件。一、几个常用...
    飘摇的水草阅读 991评论 0 0
  • 在iOS 开发中 我们有时需要判断两个UI控件是否有重合而来处理一些逻辑 举个简单的例子 看下图图片选择器 上面是...
    毛大虎阅读 3,710评论 0 3
  • 好久没来简书,竟不知道怎么发文字了。找了半天
    佟语歆阅读 215评论 0 1
  • 第8天·21天告别拖延 #玩卡不卡·每日一抽#每一位都可以通过这张卡片觉察自己: 1、直觉他叫什么名字?文静 2、...
    玉霞光阅读 176评论 0 0