来自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 物理引擎的更多相关文章

  1. 最全的iOS物理引擎demo

    概述 最全的iOS物理引擎demo,实现重力.碰撞.推力.摆动.碰撞+重力.重力弹跳.仿摩拜单车贴纸效果.防iMessage滚动效果.防百度外卖首页重力感应等效果! 详细 代码下载:http://ww ...

  2. UIDynamic仿物理引擎-浮动碰撞效果-b

    最近产品提了个需求(电商的APP-两鲜),需要在APP背景加上几个水果图案在那里无规则缓慢游荡...模仿 天天果园 APP的.好吧,那我就在网上找了很多文章,总结一下写个demo.效果如下: Mou ...

  3. iOS中的物理引擎

    目前知名的2D物理引擎有 Box2d,和Chipmunk,这些是跨平台的.但苹果本身也封装了一个物理引擎, UIDynamic是从iOS 7开始引入的一种新技术,隶属于UIKit框架.这可以让开发人员 ...

  4. iOS开发——高级篇——UIDynamic 物理引擎

    一.UIDynamic 1.简介什么是UIDynamicUIDynamic是从iOS 7开始引入的一种新技术,隶属于UIKit框架可以认为是一种物理引擎,能模拟和仿真现实生活中的物理现象重力.弹性碰撞 ...

  5. 实例介绍Cocos2d-x中Box2D物理引擎:使用关节

    下面我们将使用Box2D物理引擎技术进行重构.使得关节能够掌握如何在Box2D使用关节约束.HelloWorldScene.cpp中与使用关节的相关代码如下: void HelloWorld::add ...

  6. 实例介绍Cocos2d-x中Box2D物理引擎:碰撞检测

    在Box2D中碰撞事件通过实现b2ContactListener类函数实现,b2ContactListener是Box2D提供的抽象类,它的抽象函数:virtual void BeginContact ...

  7. 实例介绍Cocos2d-x中Box2D物理引擎:HelloBox2D

    我们通过一个实例介绍一下,在Cocos2d-x 3.x中使用Box2D物理引擎的开发过程,熟悉这些API的使用.这个实例运行后的场景如图所示,当场景启动后,玩家可以触摸点击屏幕,每次触摸时候,就会在触 ...

  8. 实例介绍Cocos2d-x物理引擎:碰撞检测

    碰撞检测是使用物理引擎的一个重要目的,使用物理引擎可以进行精确的碰撞检测,而且执行的效率也很高.在Cocos2d-x 3.x中使用事件派发机制管理碰撞事件,EventListenerPhysicsCo ...

  9. 实例介绍Cocos2d-x物理引擎:HelloPhysicsWorld

    我们通过一个实例介绍一下,在Cocos2d-x 3.x中使用物理引擎的开发过程,熟悉这些API的使用.这个实例的运行后的场景,当场景启动后,玩家可以触摸点击屏幕,每次触摸时候,就会在触摸点生成一个新的 ...

随机推荐

  1. php汉字截取

    /** * 截取HTML,并自动补全闭合 * @param $html * @param $length * @param $end */ function subHtml($html,$length ...

  2. 开源一个监控数据采集Agent:OpenFalcon-SuitAgent

    OpenFalcon-SuitAgent 项目地址:github 版本说明 本系统版本划分如下 alpha:内部测试版(不建议使用于生产环境) beta:公开测试版(不建议使用于生产环境) final ...

  3. gcc -lpthread 干什么用

    #include <stdio.h> #include <pthread.h> void *ThreadFunc(void *pArg)  //参数的值为123 { int i ...

  4. Algorithms 4th - 1.1 Basic Programming Model - EXERCISES

    欢迎交流 1.1.1 a. 7 b. 200.0000002 c. true 1.1.2 a. 1.618 b. 10.0 c. true d. 33 1.1.3 public class MainA ...

  5. cocos2dx中包含svn

    因为不想从svn上载下整个工程,就只把Classes和Resources载下来了,在打安卓包时出现WindowsError: [Error 5] : 'D:\\CocosProject\\(Proje ...

  6. 用CSS3绘制图形

    参考资料:http://blog.csdn.net/fense_520/article/details/37892507 本文非转载,为个人原创,转载请先联系博主,谢谢~ 准备: <!DOCTY ...

  7. pickle模块

    在编程中,如果存在大的列表或者字典,可以在python中引入pickle 模块: 例如:将下边这组列表保存到文件当中:[1, 2, 'xiaomao', '小狗'] 程序: import pickle ...

  8. Windows2008RT搭建VPN服务器

    总结一下2008系统搭建VPN的步骤和过程,自己有个人网站和服务要通过互联网发布出来.服务器放在自己家里,宽带是民用的.也就产生了服务发布的一些问题.用无法映射出真实的公网IP,或是一些其他内部的问题 ...

  9. 3D项目处理点选操作步骤

     1.用notepad++模型的obj格式文件,查找到模型各个部分的名称,命名规则:g mesh......,把名字改为规则命名.  2.选择处理 #ifdef _DEBUG #pragma comm ...

  10. FAQ:Python环境变量配置

    Python安装安装成,需要配置环境变量: 默认情况下,在windows下安装python之后,系统并不会自动添加相应的环境变量.此时不能在命令行直接使用python命令. 1. 首先需要在系统中注册 ...