来自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. cdh 上安装spark on yarn

    在cdh 上安装spark on yarn 还是比较简单的,不需要独立安装什么模块或者组件. 安装服务 选择on yarn 模式:上面 Spark 在spark 服务中添加 在yarn 服务中添加 g ...

  2. MySQL AUTO_INCREMENT 简介

    可使用复合索引在同一个数据表里创建多个相互独立的自增序列,具体做法是这样的:为数据表创建一个由多个数据列组成的PRIMARY KEY OR UNIQUE索引,并把AUTO_INCREMENT数据列包括 ...

  3. Stream与byte转换

    将 Stream 转成 byte[] /// <summary> /// 将 Stream 转成 byte[] /// </summary> public byte[] Str ...

  4. erlang学习笔记(2)

    函数%###geometry.erl###-module(geometry). 定义-export([area/1, function1/2, function2/0, ...]).area({rec ...

  5. selenium webdriver 学习笔记(二)

    selenium webdriver 一.定位一组元素: webdriver可以很方便的使用findElement 方法来定位某个物定的对象.不过有时候我们却要定位一组对象,这时候就需要使用findE ...

  6. @RISK

    Price: AUD $3,295.00   Price: AUD $2,495.00   适用于项目管理的 @RISK 免费试用版下载 » 立即购买 » 价格对比 » 许可选项 (英文) » 教学计 ...

  7. Group Commit of Binary Log

    160222 09:19:26 mysqld_safe Starting mysqld daemon with databases from /data01/mysql 2016-02-22 09:1 ...

  8. JavaEE Tutorials (17) - Java消息服务示例

    17.1JMS示例概述23317.2编写简单的JMS应用233 17.2.1启动JMS提供者234 17.2.2创建JMS受管理对象234 17.2.3构建所有简单示例235 17.2.4发送消息23 ...

  9. uestc Palindromic String

    字符串hash因为如果一个字符串是回文串,那么正着做哈希和反着做哈希结果应该一样.于是我们先正反各做一边哈希.如果判断出来一个字符串是回文穿那么这个字符串的前半部分和后半部分的重数一定相同,于是当前位 ...

  10. opensatck 在启动的时候注入额外的信息

    在配置ceph的时候建议使用metadata/cloud-init来注入额外的信息. https://raymii.org/s/tutorials/Automating_Openstack_with_ ...