01-UIDynamic简单演练demo源代码
-
- 源代码下载:
01-简单演练.zip
72.0 KB - //
- // ViewController.m
- // 01.简单演练
- //
- // Created by apple on 13-12-24.
- //技术博客http://www.cnblogs.com/ChenYilong/新浪微博http://weibo.com/luohanchenyilong
- #import "ViewController.h"
- @interface ViewController ()
- {
- UIDynamicAnimator *_animator;
- }
- @end
- @implementation ViewController
- /*
- 要使用UIDynamic的步骤
- 1. 实例化一个仿真者,用于物理仿真
- 参照视图:用于指定仿真的范围
- Box2D,World(仿真世界),10m
- 2. 指定要仿真(模拟)的行为(动作),并指定哪些对象遵守该动作
- 3. 将行为添加到仿真者,仿真立刻开始
- */
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- UIView *view = [[UIView alloc]initWithFrame:CGRectMake(110, 100, 100, 100)];
- view.backgroundColor = [UIColor blueColor];
- [self.view addSubview:view];
- // [view.layer setTransform:CATransform3DRotate(view.layer.transform, M_PI_4, 0, 0, 1)];
- view.transform = CGAffineTransformMakeRotation(M_PI_4);
- UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(80, 50, 50, 50)];
- redView.backgroundColor = [UIColor redColor];
- [self.view addSubview:redView];
- // 1. 谁来仿真?UIDynamicAnimator来负责仿真
- UIDynamicAnimator *animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
- // 2. 仿真个什么动作?自由落体
- UIGravityBehavior *gravity = [[UIGravityBehavior alloc] initWithItems:@[view, redView]];
- // 3. 碰撞检测
- UICollisionBehavior *collision = [[UICollisionBehavior alloc] initWithItems:@[view, redView]];
- // 设置不要出边界,碰到边界会被反弹
- collision.translatesReferenceBoundsIntoBoundary = YES;
- // 4. 开始仿真
- [animator addBehavior:gravity];
- [animator addBehavior:collision];
- //本文永久链接,转载请注明出处:http://www.cnblogs.com/ChenYilong/p/3490312.html
- // 5. 使负责仿真的UIDynamicAnimator对象时刻存在于整个事件中—>创建成员变量记录之.否则出了viewDidLoad就会被销毁.否则出了viewDidLoad就会被销毁,而仿真是个长时间持续存在的事件.
- _animator = animator;
- }
- @end
作者:
出处:http://www.cnblogs.com/ChenYilong/(点击RSS订阅)
本文版权归作者和博客园共有,欢迎转载,
但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。