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的使用.这个实例的运行后的场景,当场景启动后,玩家可以触摸点击屏幕,每次触摸时候,就会在触摸点生成一个新的 ...
随机推荐
- ORA-01157报错"cannot identify/lock data file"解决
sqlplus以管理员方式接入数据库,启动时出现报错,如下: > sqlplus "/as sysdba" SQL> startup ...... ORA-01157: ...
- c++中basic_istream::getline()的返回值何时为真
今天在看primer,17ch中的IO库再探,做课后练习,要求用ifstream.getline(char*, const unsigned, char)读取一个文件,用循环: while(ifs.g ...
- OpenCV学习(1) RGB颜色空间
1.1彩色空间 颜色是外来的光刺激作用于人的视觉器官而产生的主观感觉,它具有色调.饱和度和亮度三个特性.物体的颜色不仅取决于物体本身,还与光源.周围环境的颜色,以及观察者的视觉系统有关 1.1.1颜色 ...
- Linux学习之nl命令
nl命令在linux系统中用来计算文件中行号.nl 可以将输出的文件内容自动的加上行号!其默认的结果与 cat -n 有点不太一样, nl 可以将行号做比较多的显示设计,包括位数与是否自动补齐 0 等 ...
- Log4Net Config Appender
整理了下以前项目中使用的Log4Net的Appender. 1:只记录在一个文件下的 <appender name="RollingFileAppenderFileSize" ...
- ubuntu12.04常见错误总结
1.通过终端安装程序sudo apt-get install xxx时出错: E: Could not get lock /var/lib/dpkg/lock - open (11: Resource ...
- 关于wireshark的两个抓包过滤显示的基本语法
关于wireshark的两个基本语法 关于wireshark的两个基本语法 1. Capture Filters 语法:<Protocol name><Direction>&l ...
- 航频卫士APP截图
- windows7 64位下运行 regsvr32 注册ocx或者dll的方法
来源:转载 it won't work for you unless you have some form of Visual Basic tools loaded on your system: ...
- Prepare Python environment and install selenium.
1, Install python and selenium. I use python 3.5, the following is the example 1.) Python downloa ...