iOS开发拓展篇—UIDynamic(重力行为+碰撞检测)

一、重力行为

说明:给定重力方向、加速度,让物体朝着重力方向掉落

1.方法

(1)UIGravityBehavior的初始化

  - (instancetype)initWithItems:(NSArray *)items;

    item参数 :里面存放着物理仿真元素

(2)UIGravityBehavior常见方法

  - (void)addItem:(id <UIDynamicItem>)item;

    添加1个物理仿真元素

  - (void)removeItem:(id <UIDynamicItem>)item;

    移除1个物理仿真元素

2.UIGravityBehavior常见属性

@property (nonatomic, readonly, copy) NSArray* items;

  添加到重力行为中的所有物理仿真元素

@property (readwrite, nonatomic) CGVector gravityDirection;

  重力方向(是一个二维向量)

@property (readwrite, nonatomic) CGFloat angle;

  重力方向(是一个角度,以x轴正方向为0°,顺时针正数,逆时针负数)

@property (readwrite, nonatomic) CGFloat magnitude;

  量级(用来控制加速度,1.0代表加速度是1000 points /second²)

二、碰撞行为

1.简介

说明:可以让物体之间实现碰撞效果

  可以通过添加边界(boundary),让物理碰撞局限在某个空间中

2.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;

3.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;

  代理对象(可以监听元素的碰撞过程)

三、代码示例

在storyboard中拖拽几个控件,用于测试。

  

测试代码:

YYViewController.m文件  

 //
// YYViewController.m
// 12-重力行为和碰撞行为
//
// Created by apple on 14-8-6.
// Copyright (c) 2014年 yangyong. All rights reserved.
// #import "YYViewController.h" @interface YYViewController ()
@property (weak, nonatomic) IBOutlet UIView *redView; @property (weak, nonatomic) IBOutlet UIProgressView *block1;
@property (weak, nonatomic) IBOutlet UISegmentedControl *block2; @property(nonatomic,strong)UIDynamicAnimator *animator;
@end @implementation YYViewController
-(UIDynamicAnimator *)animator
{
if (_animator==nil) {
//创建物理仿真器(ReferenceView:参照视图,设置仿真范围)
self.animator=[[UIDynamicAnimator alloc]initWithReferenceView:self.view];
}
return _animator;
}
- (void)viewDidLoad
{
[super viewDidLoad]; //设置红色view的角度
self.redView.transform=CGAffineTransformMakeRotation(M_PI_4);
} -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//1.重力行为
// [self testGravity];
//2.重力行为+碰撞检测
// [self testGravityAndCollsion];
//3.测试重力的一些属性
[self testGravityAndCollsion2];
//用2根线作为边界
// [self testGravityAndCollision3];
//4.用圆作为边界
// [self testGravityAndCollision4];
} /**
* 重力行为
*/
-(void)testGravity
{
//1.创建仿真行为(进行怎样的仿真效果?)
//重力行为
UIGravityBehavior *gravity=[[UIGravityBehavior alloc]init];
//2.添加物理仿真元素
[gravity addItem:self.redView];
//3.执行仿真,让物理仿真元素执行仿真行为
[self.animator addBehavior:gravity];
}
/**
* 重力行为+碰撞检测
*/
-(void)testGravityAndCollsion
{
//1.重力行为
UIGravityBehavior *gravity=[[UIGravityBehavior alloc]init];
[gravity addItem:self.redView]; //2碰撞检测行为
UICollisionBehavior *collision=[[UICollisionBehavior alloc]init];
[collision addItem:self.redView];
[collision addItem:self.block1];
[collision addItem:self.block2]; //让参照视图的边框成为碰撞检测的边界
collision.translatesReferenceBoundsIntoBoundary=YES; //3.执行仿真
[self.animator addBehavior:gravity];
[self.animator addBehavior:collision];
} /**
* 测试重力行为的属性
*/
-(void)testGravityAndCollsion2
{
//1.重力行为
UIGravityBehavior *gravity=[[UIGravityBehavior alloc]init];
//(1)设置重力的方向(是一个角度)
// gravity.angle=(M_PI_2-M_PI_4);
//(2)设置重力的加速度,重力的加速度越大,碰撞就越厉害
gravity.magnitude=;
//(3)设置重力的方向(是一个二维向量)
gravity.gravityDirection=CGVectorMake(, );
[gravity addItem:self.redView]; //2碰撞检测行为
UICollisionBehavior *collision=[[UICollisionBehavior alloc]init];
[collision addItem:self.redView];
[collision addItem:self.block1];
[collision addItem:self.block2]; //让参照视图的边框成为碰撞检测的边界
collision.translatesReferenceBoundsIntoBoundary=YES; //3.执行仿真
[self.animator addBehavior:gravity];
[self.animator addBehavior:collision]; } /**
* 用圆作为边界
*/
- (void)testGravityAndCollision4
{
// 1.重力行为
UIGravityBehavior *gravity = [[UIGravityBehavior alloc] init];
[gravity addItem:self.redView]; // 2.碰撞检测行为
UICollisionBehavior *collision = [[UICollisionBehavior alloc] init];
[collision addItem:self.redView]; // 添加一个椭圆为碰撞边界
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(, , , )];
[collision addBoundaryWithIdentifier:@"circle" forPath:path]; // 3.开始仿真
[self.animator addBehavior:gravity];
[self.animator addBehavior:collision];
} /**
* 用2根线作为边界
*/
- (void)testGravityAndCollision3
{
// 1.重力行为
UIGravityBehavior *gravity = [[UIGravityBehavior alloc] init];
[gravity addItem:self.redView]; // 2.碰撞检测行为
UICollisionBehavior *collision = [[UICollisionBehavior alloc] init];
[collision addItem:self.redView];
CGPoint startP = CGPointMake(, );
CGPoint endP = CGPointMake(, );
[collision addBoundaryWithIdentifier:@"line1" fromPoint:startP toPoint:endP];
CGPoint startP1 = CGPointMake(, );
[collision addBoundaryWithIdentifier:@"line2" fromPoint:startP1 toPoint:endP];
// collision.translatesReferenceBoundsIntoBoundary = YES; // 3.开始仿真
[self.animator addBehavior:gravity];
[self.animator addBehavior:collision];
}
@end

一些测试效果:

  

代码补充说明:

(1)加速度

速度:point/s

加速度:point/s²

1\2 * 加速度 * t²

 gravity.magnitude = 1000; // 重力加速度越大,碰撞越厉害

(2)重力的方向

  坐标如下:

  

重力方向(二维向量)

说明:给定坐标平面内的一个点。然后用原点(0,0)来连接它,就构成了一个向量。

注意:在IOS中以左上角为坐标原点,向右x增加,向下Y越大。

    

     //(3)设置重力的方向(是一个二维向量)
gravity.gravityDirection=CGVectorMake(-, );

重力方向为左下角(西南)方向

(3)碰撞检测行为

  UICollisionBehavior *collision = [[UICollisionBehavior alloc] init];
[collision addItem:self.redView];
CGPoint startP = CGPointMake(, );
CGPoint endP = CGPointMake(, );
[collision addBoundaryWithIdentifier:@"line1" fromPoint:startP toPoint:endP];
CGPoint startP1 = CGPointMake(, );
[collision addBoundaryWithIdentifier:@"line2" fromPoint:startP1 toPoint:endP];

注意:标识符不能写空。可以写字符串,因为需要标识符需要遵守NSCopying协议,而字符串满足要求。

(4)贝赛尔曲线

  

提示:这里的path是一个圆,设置宽高不一样,那么得出来的就是一个椭圆。

iOS开发拓展篇—UIDynamic(重力行为+碰撞检测)的更多相关文章

  1. 李洪强iOS开发拓展篇—UIDynamic(重力行为+碰撞检测)

    iOS开发拓展篇—UIDynamic(重力行为+碰撞检测) 一.重力行为 说明:给定重力方向.加速度,让物体朝着重力方向掉落 1.方法 (1)UIGravityBehavior的初始化 - (inst ...

  2. iOS开发拓展篇—UIDynamic(简单介绍)

    iOS开发拓展篇—UIDynamic(简单介绍) 一.简单介绍 1.什么是UIDynamic UIDynamic是从iOS 7开始引入的一种新技术,隶属于UIKit框架 可以认为是一种物理引擎,能模拟 ...

  3. iOS开发拓展篇—UIDynamic(捕捉行为)

    iOS开发拓展篇—UIDynamic(捕捉行为) 一.简介 可以让物体迅速冲到某个位置(捕捉位置),捕捉到位置之后会带有一定的震动 UISnapBehavior的初始化 - (instancetype ...

  4. iOS开发拓展篇—静态库

    iOS开发拓展篇—静态库 一.简单介绍 1.什么是库? 库是程序代码的集合,是共享程序代码的一种方式 2.库的分类 根据源代码的公开情况,库可以分为2种类型 (1)开源库 公开源代码,能看到具体实现 ...

  5. iOS开发拓展篇—音效的播放

    iOS开发拓展篇—音效的播放 一.简单介绍 简单来说,音频可以分为2种 (1)音效 又称“短音频”,通常在程序中的播放时长为1~2秒 在应用程序中起到点缀效果,提升整体用户体验 (2)音乐 比如游戏中 ...

  6. iOS开发拓展篇—音乐的播放

    iOS开发拓展篇—音乐的播放 一.简单说明 音乐播放用到一个叫做AVAudioPlayer的类,这个类可以用于播放手机本地的音乐文件. 注意: (1)该类(AVAudioPlayer)只能用于播放本地 ...

  7. iOS开发拓展篇—封装音频文件播放工具类

    iOS开发拓展篇—封装音频文件播放工具类 一.简单说明 1.关于音乐播放的简单说明 (1)音乐播放用到一个叫做AVAudioPlayer的类 (2)AVAudioPlayer常用方法 加载音乐文件 - ...

  8. iOS开发拓展篇—CoreLocation简单介绍

    iOS开发拓展篇—CoreLocation简单介绍 一.简介 1.在移动互联网时代,移动app能解决用户的很多生活琐事,比如 (1)导航:去任意陌生的地方 (2)周边:找餐馆.找酒店.找银行.找电影院 ...

  9. iOS开发拓展篇—CoreLocation定位服务

    iOS开发拓展篇—CoreLocation定位服务 一.简单说明 1.CLLocationManager CLLocationManager的常用操作和属性 开始用户定位- (void)startUp ...

随机推荐

  1. Timing path

    Timing path:从register clock/input port开始,经过一些combinational logic,终止在register data/output port. PT以pa ...

  2. iOS开发Hessian

    HessianKit使用参考资料比较少,通过摸索,把测试过程贴出来,代码很乱,未整理,先实现功能,再应用到项目中.供新手参考.如有问题,跟帖指正... HessianService与Java Serv ...

  3. array_merge注意细节

    array_merge:合并一个或多个数组,一个数组中的值加在前一个数组的后面,返回的新数组作为结果 如果输入的数组中有相同的字符串键名,则该键名后面的值覆盖前面的,如果数组包含相同的数字键名,后面的 ...

  4. Spark实战4:异常检测算法Scala语言

    异常检测原理是根据训练数据的高斯分布,计算均值和方差,若测试数据样本点带入高斯公式计算的概率低于某个阈值(0.1),判定为异常点. 1 创建数据集转化工具类,把csv数据集转化为RDD数据结构 imp ...

  5. [Microsoft][ODBC 驱动程序管理器] 在指定的 DSN 中,驱动程序和应用程序之间的体系结构不匹配

    环境:  操作系统:64位WIN7   数据库:SQL Server 2000 SP1  开发语言:J2EE 在Servlet连接数据库时出错提示:  [Microsoft][ODBC 驱动程序管理器 ...

  6. 初识Python第三天(一)

    一.set集合 set是一个无序且不重复的元素集合 print(dir(set)) #['__and__', '__class__', '__contains__', '__delattr__', ' ...

  7. Codeforces 749D:Leaving Auction(set+二分)

    http://codeforces.com/contest/749/problem/D 题意:有几个人在拍卖场竞价,一共有n次喊价,有q个询问,每一个询问有一个num,接下来num个人从这次拍卖中除去 ...

  8. asp.net 之

    <script type="text/javascript"> //获取客户端实例 var pa = Sys.WebForms.PageRequestManager.g ...

  9. javax.servlet.ServletException: com.ibatis.sqlmap.client.SqlMapException: There is no statement named...问题

    可能存在3种情况: 1.在xxx.xml文件中有两个标签的id命名相同: 2.DAO实现类方法中没有写对应xxx.xml的id名称: 3.实体映射文件xxx.xml未加入到sqlMap-Config. ...

  10. css width="100" style ="width:100px" 区别

    1. width="100"是正确的,而 width="100px"是错误的, style = "width:100px"是正确的 2. s ...