5.5 iOS 碰撞行为UICollisionBehavior

1.5碰撞行为UICollisionBehavior

(一)碰撞行为UICollisionBehavior作用

作用:可以让物体之间实现碰撞效果,可以通过添加边界(boundary)将碰撞行为限定到某个区域.

(二)常用属性和方法

常用属性

@property (nonatomic, readonly, copy) NSArray<id <UIDynamicItem>> *items;
// 碰撞模式(有三种:元素碰撞,边界碰撞,全体碰撞)
// UICollisionBehaviorModeItems(元素碰撞),
// UICollisionBehaviorModeBoundaries(边界碰撞),
// UICollisionBehaviorModeEverything(全体碰撞).
@property (nonatomic, readwrite) UICollisionBehaviorMode collisionMode;
// 是否已参照视图的bounds为边界
@property (nonatomic, readwrite) BOOL translatesReferenceBoundsIntoBoundary;
// 所有的碰撞边界
@property (nullable, nonatomic, readonly, copy) NSArray<id <NSCopying>> *boundaryIdentifiers;
// 代理对象,可以监听碰撞的过程
@property (nullable, nonatomic, weak, readwrite) id <UICollisionBehaviorDelegate> collisionDelegate;

常用方法


// 初始化一个碰撞行为,items:代表碰撞行为作用的所有仿真元素
- (instancetype)initWithItems:(NSArray<id <UIDynamicItem>> *)items;
// 添加一个仿真元素到碰撞行为
- (void)addItem:(id <UIDynamicItem>)item;
// 移除碰撞行为上的一个仿真元素
- (void)removeItem:(id <UIDynamicItem>)item;
// 设置碰撞行为参照视图的边界,并且设置内边距
- (void)setTranslatesReferenceBoundsIntoBoundaryWithInsets:(UIEdgeInsets)insets;
// 添加一个路径作为碰撞行为的边界 identifier:边界ID bezierPath:使用UIBezierPath描述的一个边界
- (void)addBoundaryWithIdentifier:(id <NSCopying>)identifier forPath:(UIBezierPath *)bezierPath;
// 添加一个从p1到p2的边界 identifier:边界ID
- (void)addBoundaryWithIdentifier:(id <NSCopying>)identifier fromPoint:(CGPoint)p1 toPoint:(CGPoint)p2;
// 根据指定ID返回一个使用UIBezierPath描述的路径
- (nullable UIBezierPath *)boundaryWithIdentifier:(id <NSCopying>)identifier;
// 移除碰撞行为上指定ID为identifier的边界
- (void)removeBoundaryWithIdentifier:(id <NSCopying>)identifier;
// 移除碰撞行为上的所有边界
- (void)removeAllBoundaries;

(三)UICollisionBehaviorDelegate

// 仿真元素和仿真元素碰撞开始 item1:仿真元素1 item2:仿真元素2 p:碰撞点
- (void)collisionBehavior:(UICollisionBehavior *)behavior beganContactForItem:(id <UIDynamicItem>)item1 withItem:(id <UIDynamicItem>)item2 atPoint:(CGPoint)p;
// 仿真元素和仿真元素碰撞结束 item1:仿真元素1 item2:仿真元素2
- (void)collisionBehavior:(UICollisionBehavior *)behavior endedContactForItem:(id <UIDynamicItem>)item1 withItem:(id <UIDynamicItem>)item2;
// 仿真元素和边界碰撞开始 item:仿真元素 identifier:边界ID p:碰撞点
- (void)collisionBehavior:(UICollisionBehavior*)behavior beganContactForItem:(id <UIDynamicItem>)item withBoundaryIdentifier:(nullable id <NSCopying>)identifier atPoint:(CGPoint)p;
// 仿真元素和边界碰撞结束  item:仿真元素 identifier:边界ID
- (void)collisionBehavior:(UICollisionBehavior*)behavior endedContactForItem:(id <UIDynamicItem>)item withBoundaryIdentifier:(nullable id <NSCopying>)identifier;

示例代码:


// 控制器View
@interface RBView : UIView
@property (nonatomic, assign) CGRect rectR;

@end

@implementation RBView

- (void)drawRect:(CGRect)rect{
    UIBezierPath *path = [[UIBezierPath alloc] init];
    [path moveToPoint:CGPointMake(0, 300)];
    [path addLineToPoint:CGPointMake(200, 350)];
    [path stroke];
    [[UIBezierPath bezierPathWithRect:self.rectR] stroke];
}

@end

@interface RView : UIView
@property (nonatomic, assign) CGRect rectR;

@end

@implementation RView

- (void)drawRect:(CGRect)rect{
    [[UIBezierPath bezierPathWithRect:self.rectR] stroke];
}

@end


@interface ViewController ()<UICollisionBehaviorDelegate>
@property (nonatomic, weak) RView *redView;
@property (nonatomic, weak) UIView *blueView;
@property (nonatomic, strong) UIDynamicAnimator *animator;
@end

@implementation ViewController

- (void)loadView{
    self.view = [[RBView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    RView *redView = [[RView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    redView.backgroundColor = [UIColor redColor];
    [self.view addSubview:redView];
    UIView *blueView = [[UIView alloc] initWithFrame:CGRectMake(180, [UIScreen mainScreen].bounds.size.height - 150, 50, 50)];
    blueView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:blueView];

    self.redView = redView;
    self.blueView = blueView;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    // 1.创建仿真器
    self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
    
    // 2.创建物理仿真行为
    // 重力行为
    UIGravityBehavior *gravity = [[UIGravityBehavior alloc] initWithItems:@[self.redView]];
    // 碰撞行为
    UICollisionBehavior *collision = [[UICollisionBehavior alloc] initWithItems:@[self.redView,self.blueView]];
    // 设置边界碰撞
    collision.translatesReferenceBoundsIntoBoundary = YES;
    
    // 碰撞的action属性 实时监听碰撞过程
    collision.action = ^{
        // 需要强转
        RBView *rbView = (RBView *)self.view;
        rbView.rectR = self.redView.frame;
        [self.view setNeedsDisplay];
        NSLog(@"%@", NSStringFromCGRect(self.redView.frame));
        // 判断如果碰撞view的高大于105将颜色变成brownColor颜色 可以做一些加分什么的逻辑
        if (self.redView.frame.size.height > 105) {
            self.redView.backgroundColor = [UIColor brownColor];
        }else{
            self.redView.backgroundColor = [UIColor redColor];
        }
    };
    // 碰撞的模式
    collision.collisionMode = UICollisionBehaviorModeEverything;
    CGPoint startp = CGPointMake(0, 300);
    CGPoint endp = CGPointMake(200, 350);
    
    // 使用两点创建碰撞边界
    [collision addBoundaryWithIdentifier:@"key1" fromPoint:startp toPoint:endp];
    
    // 使用UIBezierPath创建碰撞边界
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:self.blueView.frame];
    [collision addBoundaryWithIdentifier:@"key" forPath:path];
    
    // 碰撞设置代理
    collision.collisionDelegate = self;
    
    // 3.将仿真行为添加到仿真器
    [self.animator addBehavior:gravity];
    [self.animator addBehavior:collision];
    
}
// 碰撞代理
- (void)collisionBehavior:(UICollisionBehavior *)behavior beganContactForItem:(id<UIDynamicItem>)item withBoundaryIdentifier:(id<NSCopying>)identifier atPoint:(CGPoint)p{
// 需要强制类型转化
    NSString *key = (NSString *)identifier;
    if ([key isEqualToString:@"key1"]) {
        self.redView.backgroundColor = [UIColor yellowColor];
    }else{
        self.redView.backgroundColor = [UIColor redColor];
    }
}

@end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,099评论 18 139
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 11,612评论 4 59
  • D 启航 他们的工作室成立了------安心舟工作室。 老周负责客户资源;安翎负责客户公共关系、策划兼设计;而小新...
    CPNcappuccino阅读 1,469评论 6 6
  • 寻 极地雪 蓝绿交映 迸射四裂 苍凉如洪荒 如玄黄 播下 远古的种子 那一缕轻羽 汇聚 人的模样 ...
    兰兮阅读 341评论 16 19
  • 大学已经过了五分之四,在感慨速度快的同时,也会回忆曾经美好却浅薄的宿舍情谊。 说它美好,因为最美不过初相见。结束了...
    艾书书哇阅读 227评论 4 1