常见行为:仿真&重力&碰撞&捕捉
一、UIDynamic是从iOS 7开始引入的一种新技术,隶属于UIKit框架。可以认为是一种物理引擎,能模拟和仿真现实生活中的物理现象,重力、弹性碰撞等,游戏开发中很常见,例如愤怒的小鸟。
二、UIDynamic实现物理仿真效果步骤:
(1)创建一个物理仿真器(Dynamic Animator)(顺便设置仿真范围)
(2)创建相应的物理仿真行为(Dynamic Behavior)(顺便添加物理仿真元素)
(3)将物理仿真行为添加到物理仿真器中 开始仿真
//物理仿真器:让物理仿真元素执行具体的物理仿真行为
//物理仿真行为:执行怎样的物理仿真效果?怎样的动画效果?
//物理仿真元素(Dynamic Item):谁要进行物理仿真
三、属性,方法等
UIDynamicAnimator的常见属性
@property (nonatomic, readonly) UIView* referenceView;参照视图 @property (nonatomic, readonly, copy) NSArray* behaviors;添加到物理仿真器中的所有物理仿真行为 @property (nonatomic, readonly, getter = isRunning) BOOL running;是否正在进行物理仿真 @property (nonatomic, assign) id <UIDynamicAnimatorDelegate> delegate;代理对象(能监听物理仿真器的仿真过程,比如开始和结束)
(1)能做物理仿真元素的对象:
1.任何遵守了UIDynamicItem协议的对象
2.UIView默认已经遵守了UIDynamicItem协议,因此任何UI控件都能做物理仿真
3.UICollectionViewLayoutAttributes类默认也遵守UIDynamicItem协议
(2)UIDynamic提供了以下几种物理仿真行为,所有物理仿真行为都继承自UIDynamicBehavior。都可以独立进行
1.UIGravityBehavior:重力行为
2.UICollisionBehavior:碰撞行为
3.UISnapBehavior:捕捉行为
4.UIPushBehavior:推动行为
5.UIAttachmentBehavior:附着行为
6.UIDynamicItemBehavior:动力元素行为
(4)UIDynamicAnimator的初始化:- (instancetype)initWithReferenceView:(UIView *)view;
view参数:是一个参照视图,表示物理仿真的范围
(5)添加1个物理仿真行为:- (void)addBehavior:(UIDynamicBehavior *)behavior;
(6)移除1个物理仿真行为:- (void)removeBehavior:(UIDynamicBehavior *)behavior;
(7)移除之前添加过的所有物理仿真行为:- (void)removeAllBehaviors;
注:由于以下行为与上用法相似,就不一一介绍
四、UIGravityBehavior重力行为:
UIGravityBehavior常见属性
@property (nonatomic, readonly, copy) NSArray* items;添加到重力行为中的所有物理仿真元素 @property (readwrite, nonatomic) CGVector gravityDirection;重力方向(是一个二维向量) @property (readwrite, nonatomic) CGFloat angle;重力方向(是一个角度,以x轴正方向为0°,顺时针正数,逆时针负数) @property (readwrite, nonatomic) CGFloat magnitude;量级(用来控制加速度,.0代表加速度是1000 points /second²)
UICollisionBehavior边界相关的方法
- (void)addBoundaryWithIdentifier:(id <NSCopying>)identifier forPath:(UIBezierPath*)bezierPath;
- (void)addBoundaryWithIdentifier:(id <NSCopying>)identifier fromPoint:(CGPoint)p1 toPoint:(CGPoint)p2;
- (UIBezierPath*)boundaryWithIdentifier:(id <NSCopying>)identifier;
- (void)removeBoundaryWithIdentifier:(id <NSCopying>)identifier;
@property (nonatomic, readonly, copy) NSArray* boundaryIdentifiers;
- (void)removeAllBoundaries;
五、碰撞行为:
UICollisionBehavior常见用法
@property (nonatomic, readwrite) BOOL translatesReferenceBoundsIntoBoundary;是否以参照视图的bounds为边界 - (void)setTranslatesReferenceBoundsIntoBoundaryWithInsets:(UIEdgeInsets)insets;设置参照视图的bounds为边界,并且设置内边距 @property (nonatomic, readwrite) UICollisionBehaviorMode collisionMode;碰撞模式(分为3种,元素碰撞、边界碰撞、全体碰撞) @property (nonatomic, assign, readwrite) id <UICollisionBehaviorDelegate> collisionDelegate;代理对象(可以监听元素的碰撞过程
六、捕捉行为:
简介
可以让物体迅速冲到某个位置(捕捉位置),捕捉到位置之后会带有一定的震动 UISnapBehavior的初始化
- (instancetype)initWithItem:(id <UIDynamicItem>)item snapToPoint:(CGPoint)point; UISnapBehavior常见属性
@property (nonatomic, assign) CGFloat damping;用于减幅、减震(取值范围是0. ~ 1.0,值越大,震动幅度越小) UISnapBehavior使用注意如果要进行连续的捕捉行为,需要先把前面的捕捉行为从物理仿真器中移除
七、Demo:
@interface ViewController () // 红色的View
@property (weak, nonatomic) IBOutlet UIView *redView;
// 蓝色的View
@property (weak, nonatomic) IBOutlet UIView *blueView; @property(nonatomic,strong)UIDynamicAnimator *animator; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
} - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// -1.将之前的捕捉行为移除
[self.animator removeAllBehaviors]; // 0.取出用户点击的点
CGPoint point = [[touches anyObject] locationInView:self.view]; // 1.创建捕捉行为
UISnapBehavior *snap = [[UISnapBehavior alloc] initWithItem:self.redView snapToPoint:point]; // 1.1.设置捕捉行为的阻力
snap.damping = 0.5; // 2.将仿真行为添加到仿真器中(在同一个仿真器当中,只能存在捕捉行为)
[self.animator addBehavior:snap];
} /**
* 测试重力仿真其他属性
*/
- (void)testGravity
{
// 1.创建仿真行为
NSArray *items = @[self.redView, self.blueView];
// 1.1.创建重力仿真行为,并且指定仿真元素
UIGravityBehavior *gravity = [[UIGravityBehavior alloc] initWithItems:@[self.redView]]; // 1.1.1.设置重力角度(默认是M_PI_2)
// gravity.angle = M_PI_2; // 1.1.2.设置重力的大小(默认是1.0)
// gravity.magnitude = 10.0; // 1.1.3.设置向量
gravity.gravityDirection = CGVectorMake(3.0, 1.0); // 1.2.创建碰撞行为,并且指定仿真元素
UICollisionBehavior *collision = [[UICollisionBehavior alloc] initWithItems:items]; // 1.2.1.将仿真器的bounds作为碰撞的边界
collision.translatesReferenceBoundsIntoBoundary = YES; // 2.将仿真行为添加到仿真器中,开始仿真
[self.animator addBehavior:gravity];
[self.animator addBehavior:collision];
} /**
* 测试碰撞仿真其他属性
*/
- (void)testCollision
{
// 1.创建仿真行为
NSArray *items = @[self.redView];
// 1.1.创建重力仿真行为,并且指定仿真元素
UIGravityBehavior *gravity = [[UIGravityBehavior alloc] initWithItems:items]; // 1.2.创建碰撞行为,并且指定仿真元素
UICollisionBehavior *collision = [[UICollisionBehavior alloc] initWithItems:items]; // 1.2.1.将仿真器的bounds作为碰撞的边界
collision.translatesReferenceBoundsIntoBoundary = YES; // 1.2.2.添加一个边界(线段)
CGPoint startPoint = CGPointMake(, );
CGPoint endPoint = CGPointMake(self.view.frame.size.width, );
[collision addBoundaryWithIdentifier:@"line" fromPoint:startPoint toPoint:endPoint];
// [collision removeBoundaryWithIdentifier:@"line"]; // 1.2.3.添加一个边界(贝塞尔曲线)
// UIBezierPath *bezierPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 320, 320)];
// [collision addBoundaryWithIdentifier:@"bezierPath" forPath:bezierPath]; // 2.将仿真行为添加到仿真器中,开始仿真
[self.animator addBehavior:gravity];
[self.animator addBehavior:collision];
} /**
* 重力仿真和碰撞仿真
*/
- (void)gravityAndCollision
{
// 1.创建仿真行为
NSArray *items = @[self.redView];
// 1.1.创建重力仿真行为,并且指定仿真元素
UIGravityBehavior *gravity = [[UIGravityBehavior alloc] initWithItems:items]; // 1.2.创建碰撞行为,并且指定仿真元素
UICollisionBehavior *collision = [[UICollisionBehavior alloc] initWithItems:items]; // 1.2.1.将仿真器的bounds作为碰撞的边界
collision.translatesReferenceBoundsIntoBoundary = YES; // 2.将仿真行为添加到仿真器中,开始仿真
[self.animator addBehavior:gravity];
[self.animator addBehavior:collision];
} /**
* 简单重力仿真
*/
- (void)gravity
{
/*
// 1.创建仿真器,并且指定仿真范围
UIDynamicAnimator *animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
*/
// 2.创建重力仿真行为,并且指定仿真元素
UIGravityBehavior *gravity = [[UIGravityBehavior alloc] initWithItems:@[self.redView]]; // 3.将仿真行为添加到仿真器中,开始仿真
[self.animator addBehavior:gravity];
} #pragma mark - 懒加载
- (UIDynamicAnimator *)animator
{
if (_animator == nil) {
// 创建仿真器,并且指定仿真范围
_animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
}
return _animator;
}
常见行为:仿真&重力&碰撞&捕捉的更多相关文章
- UIDynamic-吸附-重力-碰撞-物理仿真动画
现实生活中: 运动场==物理仿真器 跑步==物理仿真行为 人==仿真元素 创建步骤: 1.创建物理仿真器,并且指定仿真范围 2.创建物理仿真行为,并且指定仿真元素 3.将物理仿真行为添加到仿真器中 D ...
- UIDynamic(物理仿真)
简介 什么是UIDynamic UIDynamic是从iOS 7开始引入的一种新技术,隶属于UIKit框架 可以认为是一种物理引擎,能模拟和仿真现实生活中的物理现象 如: 重力.弹性碰撞等现象 物理引 ...
- Javascript实现重力弹跳拖拽运动效果
声明: By:GenialX 个人主页:胡旭博客 - www.ihuxu.com QQ:2252065614 演示地址: http://www.ihuxu.com/project/gcdmove/ 调 ...
- cocos2dx-3.x物理引擎Box2D介绍
理引擎 Cocos2d-x引擎内置了两种物理引擎,它们分别是Box2D和Chipmunk,都是非常优秀的2D物理引擎,而且x引擎将它们都内置在SDK中.Box2D使用较为广泛,在这里选择Box2D来进 ...
- 最全的iOS物理引擎demo
概述 最全的iOS物理引擎demo,实现重力.碰撞.推力.摆动.碰撞+重力.重力弹跳.仿摩拜单车贴纸效果.防iMessage滚动效果.防百度外卖首页重力感应等效果! 详细 代码下载:http://ww ...
- V-rep学习笔记:机器人模型创建3—搭建动力学模型
接着之前写的V-rep学习笔记:机器人模型创建2—添加关节继续机器人创建流程.如果已经添加好关节,那么就可以进入流程的最后一步:搭建层次结构模型和模型定义(build the model hierar ...
- UIDynamicBehavior的简单使用:接球小游戏
一.概念扩充: 1.在开发中,我们可以使用UIKit中提供的仿真行为,实现与现实生活中类似的物理仿真动画,UIKit动力学最大的特点是将现实世界动力驱动的动画引入了UIKit,比如重力,铰链连接,碰撞 ...
- 【Bochs 官方手册翻译】 第一章 Bochs介绍
Bochs 是一个可以完全模拟 Intel x86 计算机的虚拟机系统.它包含了 Intel x86 CPU 仿真.常见设备仿真.以及定制 BIOS.Bochs 可以虚拟多种不同类型的 x86 CPU ...
- Java处理InterruptedException异常小结
对于InterruptedException,一种常见的处理方式是捕捉它,然后什么也不做(或者记录下它,不过这也好不到哪去).不幸的是,这种方法忽略了这样一个事实:这期间可能发生中断,而中断可能导致应 ...
随机推荐
- C++11的新类型转换方法
转载自 http://blog.csdn.net/luoweifu/article/details/20493177 基于C++11标准 如果你用的编译器是基于最新的C++11标准,那么这个问题就变的 ...
- @RequesParam注解源码解析
- [Android Exception A] – 1-The following classes could not be instantiated
http://stackoverflow.com/questions/26575815/the-following-classes-could-not-be-instantiated-android- ...
- RAC_Oracle集群服务安装前期准备Prepare(案例)
2014-07-08 Created By BaoXinjian
- DBA_Oracle Erp加密和解密账户密码(案例)
2014-09-09 Created By BaoXinjian
- hdu 5441 travel 离线+带权并查集
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Problem Descript ...
- 安装完CentOS 7 后必做的七件事[转]
CentOS是最多人用来运行服务器的 Linux 版本,最新版本是 CentOS 7.当你兴趣勃勃地在一台主机或 VPS 上安装 CentOS 7 后,首要的工作肯定是加强它的安全性,以下列出的七件事 ...
- 转--Android学习笔记-实用代码合集
http://blog.csdn.net/yf210yf/article/details/7295577 转载请注明原文出处:奔跑的蜗牛(袁方的技术博客)点击打开链接 一.当利用textview显示内 ...
- hadoop对于压缩文件的支持及算法优缺点
hadoop对于压缩文件的支持及算法优缺点 hadoop对于压缩格式的是透明识别,我们的MapReduce任务的执行是透明的,hadoop能够自动为我们 将压缩的文件解压,而不用我们去关心. 如果 ...
- JAVA 处理程序异常,(try、catch、finally),(thorws)
一.try语句: try{//这里写可能出现异常的程序} catch(Exception e){//这里写如果出现异常怎么处理的程序} 二.throws语句 语法:函数方法() throws Exc ...