iOS7 UIPushBehavior

####iOS7 Programming Cookbook 第二章学习笔记 UIPushBehavior

#####Animating Your UI Components With a Push

#####ViewController.m

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
@interface ViewController ()
//方块view
@property (nonatomic, strong) UIView *squareView;
//UIDynamicAnimator,它通过底层的iOS物理引擎和动态项目之间的媒介,通过行为对象添加到动画师。
@property (nonatomic, strong) UIDynamicAnimator *animator;
//父类 UIDynamicBehavior,UIPushBehavior:推动行为持续或瞬时力量适用于一个或多个动态项目,使得这些项目改变相应位置。
@property (nonatomic, strong) UIPushBehavior *pushBehavior;
@end

@implementation ViewController

- (void) createSmallSquareView{
//创建方块 长宽
self.squareView = [[UIView alloc] initWithFrame:
CGRectMake(0.0f, 0.0f, 80.0f, 80.0f)];
//背景颜色
self.squareView.backgroundColor = [UIColor greenColor];
//初始化center
self.squareView.center = self.view.center;
//添加到view
[self.view addSubview:self.squareView];
}

- (void) createGestureRecognizer{
//创建并添加点击事件
UITapGestureRecognizer *tapGestureRecognizer =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(handleTap:)];
//点击事件添加到view
[self.view addGestureRecognizer:tapGestureRecognizer];
}

- (void) createAnimatorAndBehaviors{
self.animator = [[UIDynamicAnimator alloc]
initWithReferenceView:self.view];//指定视图为self.view

//创建碰撞检测
UICollisionBehavior *collision = [[UICollisionBehavior alloc]
initWithItems:@[self.squareView]];
//基于系统,指定一个碰撞边界
collision.translatesReferenceBoundsIntoBoundary = YES;
//创建push行为self.squareView, UIPushBehaviorModeContinuous:选择连续力量
self.pushBehavior = [[UIPushBehavior alloc]
initWithItems:@[self.squareView]
mode:UIPushBehaviorModeContinuous];
//动画师添加碰撞检测,添加push行为
[self.animator addBehavior:collision];
[self.animator addBehavior:self.pushBehavior];
}



- (void) handleTap:(UITapGestureRecognizer *)paramTap{

//从方块中心和点击点得到角度
//(CGPoint) tapPoint = (x=221, y=133.5)
CGPoint tapPoint = [paramTap locationInView:self.view];
//绿色方块当前所在位置的 方块中心坐标 (CGPoint) $1 = (x=40, y=528)
CGPoint squareViewCenterPoint = self.squareView.center;

//x ,181 = 221 - 40
CGFloat deltaX = tapPoint.x - squareViewCenterPoint.x;
//y, - 394.5 = 133.5 - 528
CGFloat deltaY = tapPoint.y - squareViewCenterPoint.y;
//atan 返回给定的 X 及 Y 坐标值的反正切值。
//反正切的角度值等于x轴正方向与通过原点和给定坐标点 (Y坐标, X坐标) 的射线之间的夹角。
//结果以弧度表示并介于 -pi 到 pi 之间(不包括 -pi)。
//-1.1406413

//检测2点之间的角度arc tangent 2((p1.x - p2.x), (p1.y - p2.y))
CGFloat angle = atan2(deltaY, deltaX);
//设置pushBehavior角的弧度
[self.pushBehavior setAngle:angle];

//从点击点和矩形
/*
距离公式: ((p1.x - p2.x)^2 + (p1.y - p2.y)^2)
*/

//pow 计算x的y次幂
//distanceBetweenPoints (434.04062)= sqrt ((181)^2+(133.5-528)^2);
CGFloat distanceBetweenPoints =
sqrt(pow(tapPoint.x - squareViewCenterPoint.x, 2.0) +
pow(tapPoint.y - squareViewCenterPoint.y, 2.0));
//设置力矢量的大小434/200
[self.pushBehavior setMagnitude:distanceBetweenPoints / 200.0f];

}

- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];

[self createGestureRecognizer];
[self createSmallSquareView];
[self createAnimatorAndBehaviors];

}

@end

Reference

坚持原创技术分享,您的支持将鼓励我继续创作!
0%