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的使用.这个实例的运行后的场景,当场景启动后,玩家可以触摸点击屏幕,每次触摸时候,就会在触摸点生成一个新的 ...
随机推荐
- Intellij Idea的一些配置
1.字体 修改IDEA面板字体:Settings->Appearance-> Override default fonts by(not recommended)选中,选择自己喜欢的字体 ...
- 获取iOS应用的版本号和app名称
NSDictionary* infoDict =[[NSBundle mainBundle] infoDictionary]; NSString* versionNum =[infoDict obje ...
- 定时器——Cocos2d-x学习历程(十一)
1.定时器 利用场景.层和精灵等游戏元素,我们可以构建游戏的框架,但是此时的游戏仍然是静止不动的.在一切游戏中,游戏的状态都会随着时间的流逝而改变,同时我们还需要定时进行一些逻辑判断,例如鱼和子弹的碰 ...
- JavaWeb核心编程之(三.1)ServletHello
Servlet简介Java Servlet是和平台无关的服务器端组件,它运行在Servlet容器中. Servlet容器负责Servlet和客户的通信以及调用Servlet方法, 在这里Servlet ...
- WEBGL 2D游戏引擎研发系列 第一章 <新的开始>
WEBGL 2D游戏引擎研发系列 第一章 <新的开始> ~\(≥▽≤)/~HTML5游戏开发者社区(群号:326492427) 转载请注明出处:http://html5gamedev.or ...
- 关于yii2的gridview关联搜索步骤
在使用yii2构建搜索视图,经常都会使用到gridview这个组件,这个组件十分强大,通过一定的配置就能进行关联搜索,下面就是简单的步骤 需求场景:一个车系表,里面存放在品牌表的id,现在要用品牌名字 ...
- Hibernate之dynamic-update
问题:设置了dynamic-update, 可是事实上并没有按照期望进行了update. 案例代码如下: 1.持久化对象 package com.jdw.hibernate.entities; imp ...
- [LeetCode]题解(python):150-Evaluate Reverse Polish Notation
题目来源: https://leetcode.com/problems/evaluate-reverse-polish-notation/ 题意分析: 给定一个数组,用这个数组来表示加减乘除,例如 [ ...
- Oracle EBS-SQL (SYS-20):职责使用菜单2.sql
select frt.responsibility_name, fr.menu_id, fm.menu_name, fr.request_group_i ...
- 交通银行万事达Y-POWER信用卡 普卡
签账消费 免息尽享 失卡保护 风险全无 密码签名 任选 境外使用 本币还款 国内海外 环球支持 适合人群:年轻一族 发行状态:发行中 年费: 140元 币种: 人民币+美元 免年费政策:免首年 ...