一、重力行為
說明:給定重力方向、加速度,讓物體朝着重力方向掉落
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檔案
1 //
2 // YYViewController.m
3 // 12-重力行為和碰撞行為
4 //
5 // Created by apple on 14-8-6.
6 // Copyright (c) 2014年 yangyong. All rights reserved.
7 //
8
9 #import "YYViewController.h"
10
11 @interface YYViewController ()
12 @property (weak, nonatomic) IBOutlet UIView *redView;
13
14 @property (weak, nonatomic) IBOutlet UIProgressView *block1;
15 @property (weak, nonatomic) IBOutlet UISegmentedControl *block2;
16
17 @property(nonatomic,strong)UIDynamicAnimator *animator;
18 @end
19
20 @implementation YYViewController
21 -(UIDynamicAnimator *)animator
22 {
23 if (_animator==nil) {
24 //建立實體仿真器(ReferenceView:參照視圖,設定仿真範圍)
25 self.animator=[[UIDynamicAnimator alloc]initWithReferenceView:self.view];
26 }
27 return _animator;
28 }
29 - (void)viewDidLoad
30 {
31 [super viewDidLoad];
32
33 //設定紅色view的角度
34 self.redView.transform=CGAffineTransformMakeRotation(M_PI_4);
35 }
36
37 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
38 {
39 //1.重力行為
40 // [self testGravity];
41 //2.重力行為+碰撞檢測
42 // [self testGravityAndCollsion];
43 //3.測試重力的一些屬性
44 [self testGravityAndCollsion2];
45 //用2根線作為邊界
46 // [self testGravityAndCollision3];
47 //4.用圓作為邊界
48 // [self testGravityAndCollision4];
49 }
50
51 /**
52 * 重力行為
53 */
54 -(void)testGravity
55 {
56 //1.建立仿真行為(進行怎樣的仿真效果?)
57 //重力行為
58 UIGravityBehavior *gravity=[[UIGravityBehavior alloc]init];
59 //2.添加實體仿真元素
60 [gravity addItem:self.redView];
61 //3.執行仿真,讓實體仿真元素執行仿真行為
62 [self.animator addBehavior:gravity];
63 }
64 /**
65 * 重力行為+碰撞檢測
66 */
67 -(void)testGravityAndCollsion
68 {
69 //1.重力行為
70 UIGravityBehavior *gravity=[[UIGravityBehavior alloc]init];
71 [gravity addItem:self.redView];
72
73 //2碰撞檢測行為
74 UICollisionBehavior *collision=[[UICollisionBehavior alloc]init];
75 [collision addItem:self.redView];
76 [collision addItem:self.block1];
77 [collision addItem:self.block2];
78
79 //讓參照視圖的邊框成為碰撞檢測的邊界
80 collision.translatesReferenceBoundsIntoBoundary=YES;
81
82 //3.執行仿真
83 [self.animator addBehavior:gravity];
84 [self.animator addBehavior:collision];
85 }
86
87 /**
88 * 測試重力行為的屬性
89 */
90 -(void)testGravityAndCollsion2
91 {
92 //1.重力行為
93 UIGravityBehavior *gravity=[[UIGravityBehavior alloc]init];
94 //(1)設定重力的方向(是一個角度)
95 // gravity.angle=(M_PI_2-M_PI_4);
96 //(2)設定重力的加速度,重力的加速度越大,碰撞就越厲害
97 gravity.magnitude=100;
98 //(3)設定重力的方向(是一個二維向量)
99 gravity.gravityDirection=CGVectorMake(0, 1);
100 [gravity addItem:self.redView];
101
102 //2碰撞檢測行為
103 UICollisionBehavior *collision=[[UICollisionBehavior alloc]init];
104 [collision addItem:self.redView];
105 [collision addItem:self.block1];
106 [collision addItem:self.block2];
107
108 //讓參照視圖的邊框成為碰撞檢測的邊界
109 collision.translatesReferenceBoundsIntoBoundary=YES;
110
111 //3.執行仿真
112 [self.animator addBehavior:gravity];
113 [self.animator addBehavior:collision];
114
115 }
116
117 /**
118 * 用圓作為邊界
119 */
120 - (void)testGravityAndCollision4
121 {
122 // 1.重力行為
123 UIGravityBehavior *gravity = [[UIGravityBehavior alloc] init];
124 [gravity addItem:self.redView];
125
126 // 2.碰撞檢測行為
127 UICollisionBehavior *collision = [[UICollisionBehavior alloc] init];
128 [collision addItem:self.redView];
129
130 // 添加一個橢圓為碰撞邊界
131 UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 320, 320)];
132 [collision addBoundaryWithIdentifier:@"circle" forPath:path];
133
134 // 3.開始仿真
135 [self.animator addBehavior:gravity];
136 [self.animator addBehavior:collision];
137 }
138
139 /**
140 * 用2根線作為邊界
141 */
142 - (void)testGravityAndCollision3
143 {
144 // 1.重力行為
145 UIGravityBehavior *gravity = [[UIGravityBehavior alloc] init];
146 [gravity addItem:self.redView];
147
148 // 2.碰撞檢測行為
149 UICollisionBehavior *collision = [[UICollisionBehavior alloc] init];
150 [collision addItem:self.redView];
151 CGPoint startP = CGPointMake(0, 160);
152 CGPoint endP = CGPointMake(320, 400);
153 [collision addBoundaryWithIdentifier:@"line1" fromPoint:startP toPoint:endP];
154 CGPoint startP1 = CGPointMake(320, 0);
155 [collision addBoundaryWithIdentifier:@"line2" fromPoint:startP1 toPoint:endP];
156 // collision.translatesReferenceBoundsIntoBoundary = YES;
157
158 // 3.開始仿真
159 [self.animator addBehavior:gravity];
160 [self.animator addBehavior:collision];
161 }
162 @end
一些測試效果:
代碼補充說明:
(1)加速度
速度:point/s
加速度:point/s²
1\2 * 加速度 * t²
gravity.magnitude = 1000; // 重力加速度越大,碰撞越厲害
(2)重力的方向
坐标如下:
重力方向(二維向量)
說明:給定坐标平面内的一個點。然後用原點(0,0)來連接配接它,就構成了一個向量。
注意:在IOS中以左上角為坐标原點,向右x增加,向下Y越大。
1 //(3)設定重力的方向(是一個二維向量)
2 gravity.gravityDirection=CGVectorMake(-1, 1);
重力方向為左下角(西南)方向
(3)碰撞檢測行為
1 UICollisionBehavior *collision = [[UICollisionBehavior alloc] init];
2 [collision addItem:self.redView];
3 CGPoint startP = CGPointMake(0, 160);
4 CGPoint endP = CGPointMake(320, 400);
5 [collision addBoundaryWithIdentifier:@"line1" fromPoint:startP toPoint:endP];
6 CGPoint startP1 = CGPointMake(320, 0);
7 [collision addBoundaryWithIdentifier:@"line2" fromPoint:startP1 toPoint:endP];
注意:辨別符不能寫空。可以寫字元串,因為需要辨別符需要遵守NSCopying協定,而字元串滿足要求。
(4)貝賽爾曲線
提示:這裡的path是一個圓,設定寬高不一樣,那麼得出來的就是一個橢圓。