一、重力行为

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

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(重力行为+碰撞检测)的更多相关文章

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

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

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

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

  3. AJ学IOS 之UIDynamic重力、弹性碰撞吸附等现象

    AJ分享,必须精品 一:效果 重力和碰撞 吸附现象 二:简介 什么是UIDynamic UIDynamic是从iOS 7开始引入的一种新技术,隶属于UIKit框架 可以认为是一种物理引擎,能模拟和仿真 ...

  4. (素材源代码) 猫猫学iOS 之UIDynamic重力、弹性碰撞吸附等现象牛逼Demo

    猫猫分享,必须精品 原创文章,欢迎转载. 转载请注明:翟乃玉的博客 地址:http://blog.csdn.net/u013357243 一:效果 二:代码 #import "ViewCon ...

  5. UIDynamic物理引擎

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

  6. 文顶顶iOS开发博客链接整理及部分项目源代码下载

    文顶顶iOS开发博客链接整理及部分项目源代码下载   网上的iOS开发的教程很多,但是像cnblogs博主文顶顶的博客这样内容图文并茂,代码齐全,示例经典,原理也有阐述,覆盖面宽广,自成系统的系列教程 ...

  7. UIDynamic--动力元素行为:UIDynamicItemBehavior

    属性分析: @property (nonatomic, readonly, copy) NSArray* items; @property (readwrite, nonatomic) CGFloat ...

  8. 3Dmax+blend+WPF综合运用

    原文:3Dmax+blend+WPF综合运用 赛后总结 本人小菜,WPF刚入门,只是写一下最近的项目心得.欢迎各位前辈们前来拍砖指正,感激不敬!先申明,小弟我入门仓促,很多东西也是一知半解,所以很多问 ...

  9. Cocos2d-js官方完整项目教程翻译:六、添加Chipmunk物理引擎在我们的游戏世界里

    添加Chipmunk物理引擎在我们的游戏世界里         一.简介                   cocos2d JS能给我们力量来创造令人印象深刻的游戏世界.但缺乏某种现实.       ...

随机推荐

  1. 协调者布局:CoordinatorLayout

    layout_scrollFlag属性: scroll:需要哪个View滚动就需要设置该属性: exitUntilCollapsed:向上推动屏幕的时候滑动的部分折叠起来,只有下滑到最低端的时候折叠部 ...

  2. 4-printf & scanf函数

    一.printf函数 这是(printf和scanf)在stdio.h中声明的一个函数,因此使用前必须加入#include <stdio.h> 1.用法 1> printf(字符串) ...

  3. SQLBackupAndFTP The server principal "NT AUTHORITY\SYSTEM" is not able to access the database "xxxx"

    Windows server 2012中使用SQLBackupAndFTP备份数据库时遇到一个错误: ERROR: The server principal "NT AUTHORITY\SY ...

  4. W3School-CSS 定位 (Positioning) 实例

    CSS 定位 (Positioning) 实例 CSS 实例 CSS 背景实例 CSS 文本实例 CSS 字体(font)实例 CSS 边框(border)实例 CSS 外边距 (margin) 实例 ...

  5. PostgreSQL-角色、库、模式、表

    由于不了解postgresql的psql工具,安装完数据库后就直接用pgadmin或navicat来连接操作,在确认初始化后的库中默认有些什么东西后竟然一直无处下手,在还没有了解pg大致体系的情况下搞 ...

  6. 解决MyEclipse中的js报错的小方法

    今天,下了个模版,但是导进去的时候发现js会报错.看了下其他都没有错误.而有一个js报错误,请原谅我有点红色强迫症,不能留一点红色 . 错误如下:Syntax error on token " ...

  7. 在虚拟机中安装红旗桌面7.0 Linux操作系统的详细图文教程

    本文作者:souvc 本文出处:http://www.cnblogs.com/liuhongfeng/p/5343087.html 以下是详细的内容: 一.安装虚拟机. 安装虚拟机可以参考:在Wind ...

  8. 关于oracle中数据类型的选择

    由于是初学,犯了如下错误: 生成表的主键id时,用当前时间的毫秒值.而在oracle中定义主键id时,用的数据类型是char(32).在mybatis中通过id取数据怎么也取不出来.想了好几天,本来以 ...

  9. Python基础算法综合:加减乘除四则运算方法

    #!usr/bin/env python# -*- coding:utf-8 -*-#python的算法加减乘除用符号:+,-,*,/来表示#以下全是python2.x写法,3.x以上请在python ...

  10. 烂泥:openvpn配置文件详解

    本文由秀依林枫提供友情赞助,首发于烂泥行天下 在上一篇文章<烂泥:ubuntu 14.04搭建OpenVPN服务器>中,我们主要讲解了openvpn的搭建与使用,这篇文章我们来详细介绍下有 ...