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;
代理对象(可以监听元素的碰撞过程)
代码:
//
// 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
UIDynamic(重力行为+碰撞检测)的更多相关文章
- iOS开发拓展篇—UIDynamic(重力行为+碰撞检测)
iOS开发拓展篇—UIDynamic(重力行为+碰撞检测) 一.重力行为 说明:给定重力方向.加速度,让物体朝着重力方向掉落 1.方法 (1)UIGravityBehavior的初始化 - (inst ...
- 李洪强iOS开发拓展篇—UIDynamic(重力行为+碰撞检测)
iOS开发拓展篇—UIDynamic(重力行为+碰撞检测) 一.重力行为 说明:给定重力方向.加速度,让物体朝着重力方向掉落 1.方法 (1)UIGravityBehavior的初始化 - (inst ...
- AJ学IOS 之UIDynamic重力、弹性碰撞吸附等现象
AJ分享,必须精品 一:效果 重力和碰撞 吸附现象 二:简介 什么是UIDynamic UIDynamic是从iOS 7开始引入的一种新技术,隶属于UIKit框架 可以认为是一种物理引擎,能模拟和仿真 ...
- (素材源代码) 猫猫学iOS 之UIDynamic重力、弹性碰撞吸附等现象牛逼Demo
猫猫分享,必须精品 原创文章,欢迎转载. 转载请注明:翟乃玉的博客 地址:http://blog.csdn.net/u013357243 一:效果 二:代码 #import "ViewCon ...
- UIDynamic物理引擎
iOS开发拓展篇—UIDynamic(简单介绍) 一.简单介绍 1.什么是UIDynamic UIDynamic是从iOS 7开始引入的一种新技术,隶属于UIKit框架 可以认为是一种物理引擎,能模拟 ...
- 文顶顶iOS开发博客链接整理及部分项目源代码下载
文顶顶iOS开发博客链接整理及部分项目源代码下载 网上的iOS开发的教程很多,但是像cnblogs博主文顶顶的博客这样内容图文并茂,代码齐全,示例经典,原理也有阐述,覆盖面宽广,自成系统的系列教程 ...
- UIDynamic--动力元素行为:UIDynamicItemBehavior
属性分析: @property (nonatomic, readonly, copy) NSArray* items; @property (readwrite, nonatomic) CGFloat ...
- 3Dmax+blend+WPF综合运用
原文:3Dmax+blend+WPF综合运用 赛后总结 本人小菜,WPF刚入门,只是写一下最近的项目心得.欢迎各位前辈们前来拍砖指正,感激不敬!先申明,小弟我入门仓促,很多东西也是一知半解,所以很多问 ...
- Cocos2d-js官方完整项目教程翻译:六、添加Chipmunk物理引擎在我们的游戏世界里
添加Chipmunk物理引擎在我们的游戏世界里 一.简介 cocos2d JS能给我们力量来创造令人印象深刻的游戏世界.但缺乏某种现实. ...
随机推荐
- [转]iOS开发中的火星坐标系及各种坐标系转换算法
iOS开发中的火星坐标系及各种坐标系转换算法 源:https://my.oschina.net/u/2607703/blog/619183 其原理是这样的:保密局开发了一个系统,能将实际的坐标转 ...
- C语言基于GTK+Libvlc实现的简易视频播放器
小编心语:现下,各种视频播放软件层出不穷,竞争也越演越烈,不知道大家有木有这个想法,小编有时在想能不能做一款属于自己的视频播放器呢~小编特意去实验楼,整理出了这篇关于如何实现简易视频播放器的博文.简易 ...
- php 日期计算 总结
1 加 2天 date("Y-m-d", strtotime("$today + 2 days")); date("Y-m-d", strt ...
- YourSQLDba开源项目发布到codeplex网站了
今天登录YourSQLDba的官方网站http://yoursqldba.grics.ca/index_en.shtml,发现YourSQLDba项目已经发布到开源网站http://www.codep ...
- 功能强大而又简单易学的编程语言Python
Python是一种面向对象.直译式计算机程序设计语言,也是一种功能强大的通用型语言.首先,Python非常简单,以Hello World为例: Java的Hello World程序一般这么写: pub ...
- Start Instance 操作详解 - 每天5分钟玩转 OpenStack(31)
本节通过日志文件详细分析 instance start 操作. 下面是 start instance 的流程图 向 nova-api 发送请求 nova-api 发送消息 nova-compute 执 ...
- Walle - 瓦力上线部署系统搭建攻略
背景知识:Walle - 瓦力是一个支持svn.git.多用户.多项目.多环境同时部署的上线部署系统,http://www.oschina.net/news/68610/walle-0-9-2 实验系 ...
- PHP-SplDoublyLinkedList
<?php /** * @package a doubly linked list test * @author zhaoyingnan<zhaoyn@bbtree.com> * @ ...
- <总结>delphi WebBrowser控件的使用中出现的bug
Delphi WebBrowser控件的使用中出现的bug: 1.WebBrowser.Visible=false:Visible属性不能使WebBrowser控件不可见,暂时用 WebBrowse ...
- 【小白的CFD之旅】01 引子
小白的CFD之旅 写在前面 CFD是计算流体力学的英文简称,是计算机辅助工程(CAE)的主要分支,目前广泛应用与科学研究.工程设计中.这是一门综合了数学.计算机及流体力学的综合学科,涉及到众多的专业理 ...