IOS 物理引擎
来自IOS7 by tutorials 下面是个人的一点翻译总结
1,首先在初始化方法李画一个方块
UIView* square = [[UIView alloc] initWithFrame:
CGRectMake(100, 100, 100, 100)];
square.backgroundColor = [UIColor grayColor]; [self.view addSubview:square];
2,声明两个变量。
UIDynamicAnimator* _animator;
UIGravityBehavior* _gravity;
3,初始化
_animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
_gravity = [[UIGravityBehavior alloc] initWithItems:@[square]]; [_animator addBehavior:_gravity];
4,闲着这个方块直接掉出屏幕了,我们需要加边框。
声明变量
UICollisionBehavior* _collision;
代码
_collision = [[UICollisionBehavior alloc] initWithItems:@[square]];
_collision.translatesReferenceBoundsIntoBoundary = YES; [_animator addBehavior:_collision];
5,多个 方块间的冲突
再声明一个长条。
UIView* barrier = [[UIView alloc]
initWithFrame:CGRectMake(0, 300, 130, 20)];
barrier.backgroundColor = [UIColor redColor]; [self.view addSubview:barrier];
我们看见方块穿过了长条。
修改代码
_collision = [[UICollisionBehavior alloc] initWithItems:@[square, barrier]];
这时长条被砸掉下去了。
6,我们把长条也弄成方框的边框。
_collision = [[UICollisionBehavior alloc] initWithItems:@[square]];
// add a boundary that coincides with the top edge
CGPoint rightEdge = CGPointMake(barrier.frame.origin.x + barrier.frame.size.width,
barrier.frame.origin.y); [_collision addBoundaryWithIdentifier:@"barrier"
fromPoint:barrier.frame.origin toPoint:rightEdge];
可以看见方框被长条弹开并落在了底部。
7,每个动画都由一个方法来执行,如碰撞,反弹等等...添加如下代码
_collision.action = ^{ NSLog(@"%@, %@",
NSStringFromCGAffineTransform(square.transform), NSStringFromCGPoint(square.center));
};
上面代码打印出方框下落时,中心和属性的变化。square.center是方框的中心,square.transform是方框的属性。
第一部分你会看到方框的中心square.center,的Y轴数据在变化,如
2014-07-24 11:41:42.133 DynamicsPlayground[766:60b] [1, 0, 0, 1, 0, 0], {150, 161}
2014-07-24 11:41:42.149 DynamicsPlayground[766:60b] [1, 0, 0, 1, 0, 0], {150, 164}
2014-07-24 11:41:42.167 DynamicsPlayground[766:60b] [1, 0, 0, 1, 0, 0], {150, 167}
当方块撞到长条时,方块开始转动,这时打印的结果是
2014-07-24 11:41:42.649 DynamicsPlayground[766:60b] [0.80305022, 0.59591132, -0.59591132, 0.80305022, 0, 0], {170, 266}
2014-07-24 11:41:42.666 DynamicsPlayground[766:60b] [0.7690773, 0.63915575, -0.63915575, 0.7690773, 0, 0], {172, 269}
2014-07-24 11:41:42.683 DynamicsPlayground[766:60b] [0.72068948, 0.69325799, -0.69325799, 0.72068948, 0, 0], {174, 272}
8,对象的动态行为是采用的 UIDynamicItem 协议 ,下面是协议的定义。
@protocol UIDynamicItem <NSObject>
@property (nonatomic, readwrite) CGPoint center;
@property (nonatomic, readonly) CGRect bounds;
@property (nonatomic, readwrite) CGAffineTransform transform;
@end
9,碰撞通知。添加代码在.m文件里。
@interface ViewController () <UICollisionBehaviorDelegate> @end
碰撞后
_collision.collisionDelegate = self;
碰撞后方法
- (void)collisionBehavior:(UICollisionBehavior *)behavior beganContactForItem:(id<UIDynamicItem>)item
withBoundaryIdentifier:(id<NSCopying>)identifier atPoint:(CGPoint)p {
NSLog(@"Boundary contact occurred - %@", identifier); }
可以看到碰撞后打印的信息,。
2014-07-24 12:15:45.124 DynamicsPlayground[820:60b] Boundary contact occurred - barrier
2014-07-24 12:15:45.692 DynamicsPlayground[820:60b] Boundary contact occurred - (null)
10,在打印后面添加代码
UIView* view = (UIView*)item; view.backgroundColor = [UIColor yellowColor];
[UIView animateWithDuration:0.3
animations:^{
view.backgroundColor = [UIColor grayColor];
}];
会看到方块在每次碰撞后发生的颜色变化。
11,配置属性。
下面代码追加在viewDidLoad方法的底部,
UIDynamicItemBehavior* itemBehaviour =
[[UIDynamicItemBehavior alloc] initWithItems:@[square]];
itemBehaviour.elasticity = 0.6;
[_animator addBehavior:itemBehaviour];
发现方块变得更有弹性了。除了弹性,itemBehaviour有更多其他属性可以更改。
12,添加动态行为
ViewController.m里添加一布尔型变量
BOOL _firstContact;
下面代码添加到协议方法里。
if (!_firstContact) {
_firstContact = YES;
UIView* square = [[UIView alloc] initWithFrame:CGRectMake(30, 0, 100, 100)];
square.backgroundColor = [UIColor grayColor];
[self.view addSubview:square];
[_collision addItem:square];
[_gravity addItem:square];
UIAttachmentBehavior* attach = [[UIAttachmentBehavior alloc]
initWithItem:view
attachedToItem:square];
[_animator addBehavior:attach];
}
两个方块被一根隐藏得线联系起来。
来自-IOS7 by tutorials,第二章第一节,目前网上有英文版下载,无中文版翻译。
--原创可转载 ,如有不恰当的地方可留言--guanliyang
IOS 物理引擎的更多相关文章
- 最全的iOS物理引擎demo
概述 最全的iOS物理引擎demo,实现重力.碰撞.推力.摆动.碰撞+重力.重力弹跳.仿摩拜单车贴纸效果.防iMessage滚动效果.防百度外卖首页重力感应等效果! 详细 代码下载:http://ww ...
- UIDynamic仿物理引擎-浮动碰撞效果-b
最近产品提了个需求(电商的APP-两鲜),需要在APP背景加上几个水果图案在那里无规则缓慢游荡...模仿 天天果园 APP的.好吧,那我就在网上找了很多文章,总结一下写个demo.效果如下: Mou ...
- iOS中的物理引擎
目前知名的2D物理引擎有 Box2d,和Chipmunk,这些是跨平台的.但苹果本身也封装了一个物理引擎, UIDynamic是从iOS 7开始引入的一种新技术,隶属于UIKit框架.这可以让开发人员 ...
- iOS开发——高级篇——UIDynamic 物理引擎
一.UIDynamic 1.简介什么是UIDynamicUIDynamic是从iOS 7开始引入的一种新技术,隶属于UIKit框架可以认为是一种物理引擎,能模拟和仿真现实生活中的物理现象重力.弹性碰撞 ...
- 实例介绍Cocos2d-x中Box2D物理引擎:使用关节
下面我们将使用Box2D物理引擎技术进行重构.使得关节能够掌握如何在Box2D使用关节约束.HelloWorldScene.cpp中与使用关节的相关代码如下: void HelloWorld::add ...
- 实例介绍Cocos2d-x中Box2D物理引擎:碰撞检测
在Box2D中碰撞事件通过实现b2ContactListener类函数实现,b2ContactListener是Box2D提供的抽象类,它的抽象函数:virtual void BeginContact ...
- 实例介绍Cocos2d-x中Box2D物理引擎:HelloBox2D
我们通过一个实例介绍一下,在Cocos2d-x 3.x中使用Box2D物理引擎的开发过程,熟悉这些API的使用.这个实例运行后的场景如图所示,当场景启动后,玩家可以触摸点击屏幕,每次触摸时候,就会在触 ...
- 实例介绍Cocos2d-x物理引擎:碰撞检测
碰撞检测是使用物理引擎的一个重要目的,使用物理引擎可以进行精确的碰撞检测,而且执行的效率也很高.在Cocos2d-x 3.x中使用事件派发机制管理碰撞事件,EventListenerPhysicsCo ...
- 实例介绍Cocos2d-x物理引擎:HelloPhysicsWorld
我们通过一个实例介绍一下,在Cocos2d-x 3.x中使用物理引擎的开发过程,熟悉这些API的使用.这个实例的运行后的场景,当场景启动后,玩家可以触摸点击屏幕,每次触摸时候,就会在触摸点生成一个新的 ...
随机推荐
- CRM后期修改实体,新增货币类型字段 需要注意的问题
货币类型字段新增 需要处理历史数据 否则编辑会报错 提示如果货币字段中存在值,则需要指定币种,请选择币种,然后重试 编辑时货币字段不显示¥符号.新增正常.第一次编辑提示错误保存后再编辑也正常.不是JS ...
- poj2459 Treasure Exploration (闭包+二分)
这道题是让求派出机器人的最少数量,乍一看以为是简单的求最小路径覆盖,后来发现错了,因为有的点可以走多次,而二分中每个点只能走一次,所以要先用floyd进行传递闭包,然后用二分 #include< ...
- C#时间日期操作
一.C# 日期格式 DateTime dt = DateTime.Now; dt.ToString();//2005-11-5 13:21:25 dt.ToFileTime().ToString() ...
- poco vs Boost[ZZ]
http://wooce.iteye.com/blog/634395 POCO的优点: 1) 比boost更好的线程库,特别是一个活动的方法的实现,并且还可设置线程的优先级. 2) 比 boost:a ...
- JavaWeb核心编程之(三.6)HttpServlet
之前都是集成的Servlet真的太过于繁琐了, Servlet接口提供了一个实现类 为HttpServlet 只要实现doGet 和doPost方法就可以了 仍然以一个表单为例 新建一个web工程 ...
- Docker基本概念填坑
Docker的基本概念填坑 Docker的基本概念填坑 1. Docker的基本组成 Docker Client客户端 Docker Daemon守护进程 Docker Image镜像 Docker ...
- nodejs 下载网页及相关资源文件
功能其实很见简单,通过 phantomjs.exe 采集 url 加载的资源,通过子进程的方式,启动nodejs 加载所有的资源,对于css的资源,匹配css内容,下载里面的url资源 当然功能还是很 ...
- oracle正则表达式regexp_like的用法详解
oracle正则表达式regexp_like的用法详解 /*ORACLE中的支持正则表达式的函数主要有下面四个:1,REGEXP_LIKE :与LIKE的功能相似2,REGEXP_INSTR :与IN ...
- TLC是什么
TLC = Triple-Level Cell,即3bit/cell,它的寿命短,速度慢,约500-5000次擦写寿命. 现在U盘多为MLC,TLC也有一部分,将来TLC会占大部分市场. 一种名为TL ...
- ANDROID SHAPE画圆形背景_ANDROID实现角标布局
ANDROID SHAPE画圆形背景_ANDROID实现角标布局 <?xml version="1.0" encoding="UTF-8"?> &l ...