一、简单介绍

1.什么是UIDynamic

UIDynamic是从iOS 7开始引入的一种新技术,隶属于UIKit框架可以认为是一种物理引擎,能模拟和仿真现实生活中的物理现象如:重力、弹性碰撞等现象

2.物理引擎的价值

广泛用于游戏开发,经典成功案例是“愤怒的小鸟”,让开发人员可以在远离物理学公式的情况下,实现炫酷的物理仿真效果提高了游戏开发效率,产生更多优秀好玩的物理仿真游戏

3.知名的2D物理引擎

Box2d

Chipmunk

二、使用步骤

要想使用UIDynamic来实现物理仿真效果,大致的步骤如下:

1.创建一个物理仿真器(顺便设置仿真范围)

2.创建相应的物理仿真行为(顺便添加物理仿真元素)

3.将物理仿真行为添加到物理仿真器中开始仿真

三、相关说明

1.三个概念

  • 谁要进行物理仿真?

      物理仿真元素(Dynamic Item)
  • 执行怎样的物理仿真效果?怎样的动画效果?

      物理仿真行为(Dynamic Behavior)
  • 让物理仿真元素执行具体的物理仿真行为

      物理仿真器(Dynamic Animator)

2.物理仿真元素

注意:

不是任何对象都能做物理仿真元素

不是任何对象都能进行物理仿真

物理仿真元素要素:

任何遵守了UIDynamicItem协议的对象

UIView默认已经遵守了UIDynamicItem协议,因此任何UI控件都能做物理仿真

UICollectionViewLayoutAttributes类默认也遵守UIDynamicItem协议

3.物理仿真行为

(1)UIDynamic提供了以下几种物理仿真行为

  • UIGravityBehavior:重力行为

  • UICollisionBehavior:碰撞行为

  • UISnapBehavior:捕捉行为

  • UIPushBehavior:推动行为

  • UIAttachmentBehavior:附着行为

  • UIDynamicItemBehavior:动力元素行为

(2)物理仿真行为须知

上述所有物理仿真行为都继承自UIDynamicBehavior,所有的UIDynamicBehavior都可以独立进行组合使用多种行为时,可以实现一些比较复杂的效果

4.物理仿真器

(1)物理仿真器须知

它可以让物理仿真元素执行物理仿真行为

它是UIDynamicAnimator类型的对象

(2)UIDynamicAnimator的初始化

  1. - (instancetype)initWithReferenceView:(UIView *)view;

view参数:是一个参照视图,表示物理仿真的范围

5.物理仿真器的说明

(1)UIDynamicAnimator的常见方法

  1. //移除之前添加过的所有物理仿真行为
  2. - (void)addBehavior:(UIDynamicBehavior *)behavior;
  3. //移除之前添加过的所有物理仿真行为 
  4. - (void)removeBehavior:(UIDynamicBehavior *)behavior;
  5. //添加1个物理仿真行为 
  6. - (void)removeAllBehaviors;

 

(2)UIDynamicAnimator的常见属性

  1. //参照视图
  2. @property (nonatomic, readonly) UIView* referenceView;
  3. //添加到物理仿真器中的所有物理仿真行为
  4. @property (nonatomic, readonly, copy) NSArray* behaviors;
  5. //是否正在进行物理仿真
  6. @property (nonatomic, readonly, getter = isRunning) BOOL running;
  7. //代理对象(能监听物理仿真器的仿真过程,比如开始和结束)
  8. @property (nonatomic, assign) id

四、案例介绍

先看效果吧

  • UIGravityBehavior:重力行为

  • UICollisionBehavior:碰撞行为

  • UISnapBehavior:捕捉行为

  • UIPushBehavior:推动行为

  • 复合效果

最后是代码

  1. #import <UIKit/UIKit.h>
  2. @interface SecondViewController : UIViewController
  3. @property (weak, nonatomic) IBOutlet UIView *blueView;
  4. @property (weak, nonatomic) IBOutlet UIView *orangeView;
  5. @property (weak, nonatomic) IBOutlet UISegmentedControl *segmented;
  6. @property (weak, nonatomic) IBOutlet UIView *redView;
  7. @property (nonatomic,strong) UIDynamicAnimator *animator;
  8. @end
  9. #import "SecondViewController.h"
  10. @interface SecondViewController ()
  11. @property (nonatomic,strong)UISnapBehavior *snapBehavior;
  12. @property (nonatomic,strong)UIPushBehavior *pushBehavior;
  13. @property (nonatomic,strong)UIAttachmentBehavior *attachmentBehavior;
  14. @end
  15. @implementation SecondViewController
  16. - (void)viewDidLoad {
  17. [super viewDidLoad];
  18. self.blueView.transform = CGAffineTransformMakeRotation(M_PI_4);
  19. self.segmented.transform = CGAffineTransformMakeRotation(-M_PI / 8);
  20. if (!_pushBehavior) {
  21. _pushBehavior = [[UIPushBehavior alloc]initWithItems:@[_blueView] mode:UIPushBehaviorModeContinuous];
  22. }
  23. _pushBehavior.active = YES;
  24. _pushBehavior.pushDirection = CGVectorMake(10.0f, 10.0f);
  25. _pushBehavior.magnitude = 1.0f;
  26. [self.animator addBehavior:_pushBehavior];
  27. }
  28. - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent: (UIEvent *)event {
  29. _pushBehavior.active = NO;
  30. //创建重力行为
  31. UIGravityBehavior *gravityBehavior = [[UIGravityBehavior alloc]init];
  32. [gravityBehavior addItem:self.blueView];
  33. [gravityBehavior addItem:self.orangeView];
  34. [gravityBehavior addItem:self.segmented];
  35. gravityBehavior.gravityDirection = CGVectorMake(-0.3f, 1.0f);
  36. //加速度
  37. gravityBehavior.magnitude = 3;
  38. //创建碰撞行为
  39. UICollisionBehavior *collisionBehavior = [[UICollisionBehavior alloc]init];
  40. //碰撞类型为元素和边界
  41. collisionBehavior.collisionMode = UICollisionBehaviorModeEverything;
  42. CGFloat Y = self.view.frame.size.height - CGRectGetHeight(self.redView.frame);
  43. CGFloat X = self.view.frame.size.width;
  44. CGFloat height = self.view.frame.size.height;
  45. //设置红色的View为底边界,左边框跟右边框作为边界
  46. [collisionBehavior addBoundaryWithIdentifier:@"collision1" fromPoint:CGPointMake(0,Y) toPoint:CGPointMake(X, Y)];
  47. [collisionBehavior addBoundaryWithIdentifier:@"collision2" fromPoint:CGPointMake(0, 0) toPoint:CGPointMake(0, height)];
  48. [collisionBehavior addBoundaryWithIdentifier:@"collision3" fromPoint:CGPointMake(X,0) toPoint:CGPointMake(X, height)];
  49. [collisionBehavior addItem:self.blueView];
  50. [collisionBehavior addItem:self.segmented];
  51. [collisionBehavior addItem:self.orangeView];
  52. [self.animator addBehavior:collisionBehavior];
  53. [self.animator addBehavior:gravityBehavior];
  54. UIDynamicItemBehavior *itemBehavior = [[UIDynamicItemBehavior alloc] initWithItems:@[self.blueView]];
  55. [itemBehavior setElasticity:0.5];
  56. [self.animator addBehavior:itemBehavior];
  57. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGesture:)];
  58. [self.view addGestureRecognizer:tap];
  59. }
  60. - (void)tapGesture:(UITapGestureRecognizer *)gesture
  61. {
  62. CGPoint tapPoint = [gesture locationInView:self.view];
  63. if (_snapBehavior) {
  64. [self.animator removeBehavior:_snapBehavior];
  65. _snapBehavior = nil;
  66. }
  67. _snapBehavior = [[UISnapBehavior alloc]initWithItem:self.blueView snapToPoint:tapPoint];
  68. _snapBehavior.action = ^(){
  69. NSLog(@"UISnapBehavior 在执行");
  70. };
  71. _snapBehavior.damping = 0.9;
  72. [self.animator addBehavior:_snapBehavior];
  73. }
  74. - (UIDynamicAnimator *)animator{
  75. if (_animator == nil) {
  76. _animator = [[UIDynamicAnimator alloc]init];
  77. }
  78. return _animator;
  79. }
  80. - (void)panGestureRecognizer:(UIPanGestureRecognizer *)gesture
  81. {
  82. CGPoint location = [gesture locationInView:self.view];
  83. CGPoint imageLocation = [gesture locationInView:self.orangeView];
  84. switch (gesture.state) {
  85. case UIGestureRecognizerStateBegan:
  86. {
  87. NSLog(@"touch position %@",NSStringFromCGPoint(location));
  88. NSLog(@"loction in image %@",NSStringFromCGPoint(imageLocation));
  89. [self.animator removeAllBehaviors];
  90. UIOffset centerOffset = UIOffsetMake(imageLocation.x - CGRectGetMidX(self.orangeView.bounds), imageLocation.y - CGRectGetMidY(self.orangeView.bounds));
  91. _attachmentBehavior = [[UIAttachmentBehavior alloc]initWithItem:self.orangeView offsetFromCenter:centerOffset attachedToAnchor:location];
  92. _attachmentBehavior.damping = 0.5;
  93. _attachmentBehavior.frequency = 0.8;
  94. [self.animator addBehavior:_attachmentBehavior];
  95. }
  96. break;
  97. case UIGestureRecognizerStateEnded:
  98. {
  99. [self.animator removeBehavior:_attachmentBehavior];
  100. }
  101. break;
  102. default:
  103. {
  104. [_attachmentBehavior setAnchorPoint:[gesture locationInView:self.view]];
  105. }
  106. break;
  107. }
  108. }
  109. - (void)didReceiveMemoryWarning {
  110. [super didReceiveMemoryWarning];
  111. // Dispose of any resources that can be recreated.
  112. }
  113. /*
  114. #pragma mark - Navigation
  115. // In a storyboard-based application, you will often want to do a little preparation before navigation
  116. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  117. // Get the new view controller using [segue destinationViewController].
  118. // Pass the selected object to the new view controller.
  119. }
  120. */
  121. @end

UIKit动力学的部分介绍完了.

最开始项目中只是用到了重力跟碰撞行为,参考学习了:UIDynamic 详细用法 中的案例.然后自己又展开了解了下UIKit动力学的知识,把捕捉行为,推动行为加了进去,做个完善.整理了一下.就是这样.

带你了解UIKit动力学的更多相关文章

  1. iOS UIKit Dynamics入门 UIKit动力学,实现重力、连接、碰撞、悬挂等动画效果

    本文为转载文章 版权归原文所有 什么是UIKit动力学(UIKit Dynamics) 其实就是UIKit的一套动画和交互体系.我们现在进行UI动画基本都是使用CoreAnimation或者UIVie ...

  2. UIDynamicAnimator UIKit动力学

    也许是工作上并没有这方面的需要,对UIDynamicAnimator的了解不多.这里做简单的介绍: UIKit动力学是模拟真实世界的一些特性,主要就是UIDynamicAnimator类,通过类中的不 ...

  3. iOS开发概述UIkit动力学,讲述UIKit的Dynamic特性,UIkit动力学是UIkit框架中模拟真实世界的一些特性。

    转发:http://my.oschina.net/u/1378445/blog/335014 iOS UIKit动力学 Dynamics UIAttachmentBehavior 实现iMessage ...

  4. iOS7 UIKit动力学-碰撞特性UICollisionBehavior 下

    上文讲到了为window加一个边界.实现碰撞的效果,接下来我们将提到一个托付方法: - (void)collisionBehavior:(UICollisionBehavior *)behavior ...

  5. iOS7 UIKit动力学-重力特性UIGravityBehavior

    续文 在iOS7中事实上新加了非常多新的特性.之前看过,也了解过一些新的内容.如新的动力学特性,TextKit的图文混排,还有自己定义的动画跳转等.那段时间也比較忙,没时间整理.如今项目也弄完了,打算 ...

  6. iOS7 UIKit动力学-碰撞特性UICollisionBehavior 上

    我们谈到了重力上述财产UIGravityBehavior这个类. 非常明确的看法,当我们添加的属性的严重性后,,苹果UIview像掉进无底洞,地下坠,不断的加速.而如今呢,我们要在这个手机屏幕上,加入 ...

  7. iOS开发UIkit动力学UIDynamicAnimator一系列动画

    UIDynamicAnimator类,通过这个类中的不同行为来实现一些动态特性. UIAttachmentBehavior(吸附),UICollisionBehavior(碰撞),UIGravityB ...

  8. WWDC 2013 Session笔记 - UIKit Dynamics入门

    本文涉及到的WWDC2013 Session有 1.Session 206 Getting Started with UIKit Dynamics 2.Session 221 Advanced Tec ...

  9. ios UIKit动力

    UIkit动力学是UIkit框架中模拟真实世界的一些特性. UIDynamicAnimator 主要有UIDynamicAnimator类,通过这个类中的不同行为来实现一些动态特性. 它一般有两种初始 ...

随机推荐

  1. 【MySQL笔记】Excel数据导入Mysql数据库的实现方法——Navicat

    很多公司尤其有点年头的公司,财务业务部门的各种表单都是excel来做的表格,随着互联网的发展各种业务流程都电子化流程化了,再在茫茫多的文档中去查找某一个年份月份的报告是件相当枯燥的事,所以都在想办法将 ...

  2. Visual Studio 控制台应用程序 同时使用OpenCV和matlab mat文件操作

    matalb具有灵活的图像处理,代码编写起来简洁而高效.而OpenCV具有很多成熟的计算机视觉算法,能够处理很多实时的识别处理等问题,而且代码运行起来效率很高.所以如何结合两者之间的优点,是让很多学术 ...

  3. 微信php分享页面自定义标题与内容

    1.因为现在分享页面,发给朋友或者朋友圈都没办法自定义标题.图片和内容,所以必须要有微信公众号 2.如果有微信公众号可直接登录,如果没有要注册,注册完或者登录了 3.查看你的权限,左侧最下面开发的接口 ...

  4. php漏洞挖掘书籍

    PHP是一种被广泛使用的脚本语言,尤其适合web开发.具有跨平台,容易学习,功能强大等特点,据统计全世界超过34%的网站有php的应用,包括Yahoo.sina.163.sohu等大型门户网站.而且很 ...

  5. min-height IE6的解决方案

    selector { min-height:500px; height:auto !important; height:500px; } 因为IE6中当内容大于容器高度或宽度时,就会撑破容器.并且在同 ...

  6. C# SendMail 发送邮件

    最近因为用的发送邮件的地方,就查询了资料,总结以下几个方法 1.利用新浪邮箱发送 2.利用公司邮箱发送 3.利用CDO发送,这种方式要引用Interop.ADODB.dll(http://www.no ...

  7. HAWQ技术解析(十八) —— 问题排查

    (原文地址:http://hawq.incubator.apache.org/docs/userguide/2.1.0.0-incubating/troubleshooting/Troubleshoo ...

  8. rails generate model/resource/scaffold的区别

    If you’re just learning Ruby on Rails, you may be confused as to when to generate individual models, ...

  9. Easypack容器系列之:Nexus 3:Docker私库

    Nexus作为私库管理最为流行的工具之中的一个,用于包的管理和Docker镜像管理的私库管理场景中非经常常使用.Easypack利用最新版本号的oss版Nexus作为基础镜像用于提供相似服务. 本文将 ...

  10. selenium从入门到应用 - 5,页面对象设计模式下的页面模块

    本系列所有代码 https://github.com/zhangting85/simpleWebtest 本文将介绍一个Java+TestNG+Maven+Selenium的web自动化测试脚本环境下 ...