一、重力行为

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

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;

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

代码:

 //
// 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];
// [gravity addItem:self.block1];
// [gravity addItem:self.block2]; // 2.碰撞检测行为
UICollisionBehavior *collision = [[UICollisionBehavior alloc] init];
[collision addItem:self.redView];
[collision addItem:self.block1];
[collision addItem:self.block2]; // 添加一个椭圆为碰撞边界
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];
[collision addItem:self.block1];
[collision addItem:self.block2];
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. 漫画告诉你什么是DDoS攻击?
  2. HDU 4049 Tourism Planning(动态规划)
  3. C语言预处理操作符
  4. VB操作CAD
  5. 最全面的Android Intent机制讲解
  6. Unity KGFMapSystem插件制作小地图
  7. Android 中如何使用动画
  8. 举例android项目中的string.xml出现这个The character reference must end with the &#39;;&#39; delimiter.错误提示的原因及解决办法
  9. c#并行任务多种优化方案分享(异步委托)
  10. python遍历字典元素
  11. Mysql,zip格式安装、修改密码、建库
  12. Eclipse工程有乱码
  13. 201521123042《Java程序》第二周总结
  14. JavaScript 新语法详解:Class 的私有属性与私有方法
  15. 抛弃配置后的Spring终极教程
  16. 深入理解javascript原型和闭包——从【自由变量】到【作用域链】
  17. Requests将verify设置为False后取消警告的方式
  18. ASP.NET Core 2.2中的Endpoint路由
  19. PHP从入门到精通(三)
  20. sublime text 3 笔记 简单配置

热门文章

  1. Windows操作系统优化(Win7版) - 进阶者系列 - 学习者系列文章
  2. js自动完成
  3. 带你一步步的了解“C#事件”机制
  4. Google在KDD2013上关于CTR的一篇论文
  5. forward和redirect的区别(转)
  6. 机器学习之寻找KMeans的最优K
  7. mysql 常用sql
  8. linux shell 比较文件夹内容 diff
  9. 在macOS Sierra 10.12搭建PHP开发环境
  10. linux常用系统监控命令