iOS7 UIGravityBehavior

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

#####Adding Gravity to your UI components

#####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

@interface ViewController ()
//声明方块变量
@property (nonatomic, strong) UIView *squareView;
//声明动画师
@property (nonatomic, strong) UIDynamicAnimator *animator;
@end

@implementation ViewController

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

//创建方形view,and添加的view,长宽100
self.squareView = [[UIView alloc] initWithFrame:
CGRectMake(0.0f, 0.0f, 100.0f, 100.0f)];
//添加背景颜色绿色
self.squareView.backgroundColor = [UIColor greenColor];
//初始化center
self.squareView.center = self.view.center;
//方形添加到view
[self.view addSubview:self.squareView];

//创建动画师和重力
self.animator = [[UIDynamicAnimator alloc]
initWithReferenceView:self.view];
//创建重力行为,为方形
UIGravityBehavior *gravity = [[UIGravityBehavior alloc]
initWithItems:@[self.squareView]];
//动画师添加行为:gravity
[self.animator addBehavior:gravity];

}

@end

Reference

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