iOS UIDynamic简介 —— HERO博客
2015-12-31 10:34
477 查看
UIDynamic简介:
UIDynamic是从iOS 7开始引入的一种技术,是UIKit提供的非常方便的API,类似物理引擎效果,可以模仿重力、碰撞、反弹等效果。
UIDynamic包含几个主要属性:
1. 行为:
UIGravityBehavior:重力行为
UICollisionBehavior:碰撞行为
UISnapBehavior:捕捉行为
UIPushBehavior:推动行为
UIAttachmentBehavior:附着行为
UIDynamicItemBehavior:动力元素行为
2. 对象:
遵守了UIDynamicItem协议的对象,如UIView、UICollectionViewLayoutAttributes类默认已经遵守了UIDynamicItem协议,因此任何UI控件都能做物理仿真。
3.物理仿真器:
让对象执行仿真行为。
UIDynamic使用:
写了个小列子,简单说明一下UIDynamic的使用,先看一下效果图:
下面贴上代码:
UIDynamic是从iOS 7开始引入的一种技术,是UIKit提供的非常方便的API,类似物理引擎效果,可以模仿重力、碰撞、反弹等效果。
UIDynamic包含几个主要属性:
1. 行为:
UIGravityBehavior:重力行为
UICollisionBehavior:碰撞行为
UISnapBehavior:捕捉行为
UIPushBehavior:推动行为
UIAttachmentBehavior:附着行为
UIDynamicItemBehavior:动力元素行为
2. 对象:
遵守了UIDynamicItem协议的对象,如UIView、UICollectionViewLayoutAttributes类默认已经遵守了UIDynamicItem协议,因此任何UI控件都能做物理仿真。
3.物理仿真器:
让对象执行仿真行为。
UIDynamic使用:
写了个小列子,简单说明一下UIDynamic的使用,先看一下效果图:
下面贴上代码:
#import <UIKit/UIKit.h> @interface ViewController : UIViewController @end #import "ViewController.h" #define mainW [UIScreen mainScreen].bounds.size.width #define mainH [UIScreen mainScreen].bounds.size.height @interface ViewController () @property (nonatomic, strong) UIDynamicAnimator *animator; @property (nonatomic, weak) UIView *redView; @property (nonatomic, weak) UIView *yellowView; @property (nonatomic, weak) UIView *greenView; @property (nonatomic, weak) UIView *blueView; @end @implementation ViewController - (UIDynamicAnimator *)animator { if (_animator == nil) { //创建物理仿真器 _animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view]; } return _animator; } - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor blackColor]; //创建控件 [self creatControl]; } - (void)creatControl { UIView *blueView = [[UIView alloc] initWithFrame:CGRectMake((mainW - 51) * 0.5, mainH - 51, 51, 51)]; for (int i = 0; i < 51; i++) { UIView *line = [[UIView alloc] initWithFrame:CGRectMake(26 - i, i, 2 * i + 1, 1)]; line.backgroundColor = [UIColor blueColor]; [blueView addSubview:line]; } [self.view addSubview:blueView]; self.blueView = blueView; UIView *yellowView = [[UIView alloc] initWithFrame:CGRectMake((mainW - 200) * 0.5, CGRectGetMinY(blueView.frame) - 20, 200, 20)]; yellowView.backgroundColor = [UIColor yellowColor]; [self.view addSubview:yellowView]; self.yellowView = yellowView; UIView *greenView = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMinX(yellowView.frame), CGRectGetMinY(yellowView.frame) - 40, 40, 40)]; greenView.backgroundColor = [UIColor greenColor]; greenView.layer.cornerRadius = 20; greenView.clipsToBounds = YES; [self.view addSubview:greenView]; self.greenView = greenView; UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(yellowView.frame) - 50, CGRectGetMinY(yellowView.frame) - 50, 50, 50)]; redView.backgroundColor = [UIColor redColor]; [self.view addSubview:redView]; self.redView = redView; UIButton *resetBtn = [[UIButton alloc] initWithFrame:CGRectMake((mainW - 100) * 0.5, 50, 100, 30)]; resetBtn.backgroundColor = [UIColor orangeColor]; [resetBtn setTitle:@"重置" forState:UIControlStateNormal]; [resetBtn addTarget:self action:@selector(resetBtnOnClick) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:resetBtn]; } - (void)resetBtnOnClick { for (UIControl *control in self.view.subviews) { [control removeFromSuperview]; } [self creatControl]; } - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { //获取一个触摸点 UITouch *touch = [touches anyObject]; CGPoint point = [touch locationInView:self.view]; //创建捕捉行为 UISnapBehavior *snap = [[UISnapBehavior alloc] initWithItem:self.redView snapToPoint:point]; //设置防震系数(0~1,数值越大,震动的幅度越小) snap.damping = 0.5; //删除之前的所有仿真行为 [self.animator removeAllBehaviors]; //执行捕捉行为 [self.animator addBehavior:snap]; //设置0.5秒延时执行 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.5f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ //重力碰撞 [self GravityCollisionStart]; }); } -(void)GravityCollisionStart { //重力行为 UIGravityBehavior *gravity = [[UIGravityBehavior alloc] init]; //设置重力的加速度,重力的加速度越大,碰撞就越厉害 gravity.magnitude = 10; //设置重力的方向(水平,垂直) gravity.gravityDirection = CGVectorMake(0, 10); [gravity addItem:self.redView]; //碰撞行为 UICollisionBehavior *collision = [[UICollisionBehavior alloc] init]; [collision addItem:self.redView]; [collision addItem:self.yellowView]; [collision addItem:self.greenView]; [collision addItem:self.blueView]; //让参照视图的边框成为碰撞检测的边界 collision.translatesReferenceBoundsIntoBoundary = YES; //执行仿真 [self.animator removeAllBehaviors]; [self.animator addBehavior:gravity]; [self.animator addBehavior:collision]; } @end
相关文章推荐
- 【UIL框架】Universal-Image-Loader完全解析(一)之介绍与使用详解
- UIPicView地区选择器
- iOS 9 学习系列:UIStack View (转载)
- Android UI模板设计
- 用Android UEventObserver监听内核event
- iOS真机UI调试利器——Reveal
- 使用groovy JsonBuilder输出对象JSON格式
- iOS改变UIImageView的image时添加渐变,平滑改变图片
- cell自动计算高度(masonry与UITableView-FDTemplateLayoutCell)
- 怎样实现UIView的旋转
- UIFont的使用和字体类型总结
- mac下flash builder 4.6/4.7 升级 airsdk的版本至3.8的方法
- iOS - 用 UIBezierPath 实现果冻效果
- FlashBuilder的air sdk和FlashPlayer升级方法
- String.valueOf(a)和(String)a有什么区别
- UIButton关闭交互的三种实现方法
- LR的web_url、web_custom_request、web_submit_data、web_submit_form应用实例
- JAVA图形界面(GUI)
- iOS_UIButton 简单操作
- UITableView的全部属性、方法以及代理方法执行顺序