iOS7 UIDynamicItemBehavior

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

#####Assigning Characteristic to Your Dynamic Effects

#####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
@interface ViewController ()
//创建动画师
@property (nonatomic, strong) UIDynamicAnimator *animator;
@end

@implementation ViewController

/**
* 创建view并设置背景颜色
*
* @param paramCenter CGPoint
* @param paramBackgroundColor UIColor
*
* @return 返回view
*/

- (UIView *) newViewWithCenter:(CGPoint)paramCenter
backgroundColor:(UIColor *)paramBackgroundColor{
//创建newView 长宽50
UIView *newView = [[UIView alloc] initWithFrame:
CGRectMake(0.0f, 0.0f, 50.0f, 50.0f)];
//背景设置paramBackgroundColor
newView.backgroundColor = paramBackgroundColor;
//中心点
newView.center = paramCenter;
//返回view
return newView;
}

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

//topView创建在指定的坐标点,设置成绿色
UIView *topView = [self newViewWithCenter:CGPointMake(100.0f, 0.0f)
backgroundColor:[UIColor greenColor]];
//bottomView创建指定坐标(100,50),设置成红色
UIView *bottomView = [self newViewWithCenter:CGPointMake(100.0f, 50.0f)
backgroundColor:[UIColor redColor]];

//添加到topView
[self.view addSubview:topView];
//添加到bottomView
[self.view addSubview:bottomView];
//初始化动态动画师到self.view
self.animator = [[UIDynamicAnimator alloc]
initWithReferenceView:self.view];
//创建重力行为为topView,bottomView
UIGravityBehavior *gravity = [[UIGravityBehavior alloc]
initWithItems:@[topView, bottomView]];
//动画师添加行为为:gravity
[self.animator addBehavior:gravity];

//创建碰撞检测
UICollisionBehavior *collision = [[UICollisionBehavior alloc]
initWithItems:@[topView, bottomView]];
//基于系统,指定一个碰撞边界
collision.translatesReferenceBoundsIntoBoundary = YES;
//动画师添加碰撞行为
[self.animator addBehavior:collision];

//UIDynamicItemBehavior,动态项行为代表一个或多个动态项动画配置
//指定项目弹性
UIDynamicItemBehavior *moreElasticItem = [[UIDynamicItemBehavior alloc]
initWithItems:@[bottomView]];
//elasticity,弹性量应用于碰撞检测行为
moreElasticItem.elasticity = 1.0f;

UIDynamicItemBehavior *lessElasticItem = [[UIDynamicItemBehavior alloc]
initWithItems:@[topView]];
//elasticity,弹性量应用于碰撞检测行为
lessElasticItem.elasticity = 0.5f;
//动画师添加 多弹性,少弹性
[self.animator addBehavior:moreElasticItem];
[self.animator addBehavior:lessElasticItem];

}

@end

Reference

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