iOS开发拓展篇—UIDynamic(捕捉行为)
2016-06-07 11:33
411 查看
iOS开发拓展篇—UIDynamic(捕捉行为)
一、简介
可以让物体迅速冲到某个位置(捕捉位置),捕捉到位置之后会带有一定的震动
UISnapBehavior的初始化
- (instancetype)initWithItem:(id <UIDynamicItem>)item snapToPoint:(CGPoint)point;
UISnapBehavior常见属性
@property (nonatomic, assign) CGFloat damping;
用于减幅、减震(取值范围是0.0 ~ 1.0,值越大,震动幅度越小)
UISnapBehavior使用注意
如果要进行连续的捕捉行为,需要先把前面的捕捉行为从物理仿真器中移除
二、代码说明
在storyboard中放一个view控件,作为演示用的仿真元素。
代码如下:
一、简介
可以让物体迅速冲到某个位置(捕捉位置),捕捉到位置之后会带有一定的震动
UISnapBehavior的初始化
- (instancetype)initWithItem:(id <UIDynamicItem>)item snapToPoint:(CGPoint)point;
UISnapBehavior常见属性
@property (nonatomic, assign) CGFloat damping;
用于减幅、减震(取值范围是0.0 ~ 1.0,值越大,震动幅度越小)
UISnapBehavior使用注意
如果要进行连续的捕捉行为,需要先把前面的捕捉行为从物理仿真器中移除
二、代码说明
在storyboard中放一个view控件,作为演示用的仿真元素。
代码如下:
// // YYViewController.m // 13-捕捉行为 // // Created by apple on 14-8-8. // Copyright (c) 2014年 yangyong. All rights reserved. // #import "YYViewController.h" @interface YYViewController () @property (weak, nonatomic) IBOutlet UIView *blueView; @property(nonatomic,strong)UIDynamicAnimator *animator; @end @implementation YYViewController -(UIDynamicAnimator *)animator { if (_animator==nil) { //创建物理仿真器,设置仿真范围,ReferenceView为参照视图 _animator=[[UIDynamicAnimator alloc]initWithReferenceView:self.view]; } return _animator; } - (void)viewDidLoad { [super viewDidLoad]; } -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { //获取一个触摸点 UITouch *touch=[touches anyObject]; CGPoint point=[touch locationInView:touch.view]; //1.创建捕捉行为 //需要传入两个参数:一个物理仿真元素,一个捕捉点 UISnapBehavior *snap=[[UISnapBehavior alloc]initWithItem:self.blueView snapToPoint:point]; //设置防震系数(0~1,数值越大,震动的幅度越小) snap.damping=arc4random_uniform(10)/10.0; //2.执行捕捉行为 //注意:这个控件只能用在一个仿真行为上,如果要拥有持续的仿真行为,那么需要把之前的所有仿真行为删除 //删除之前的所有仿真行为 [self.animator removeAllBehaviors]; [self.animator addBehavior:snap]; } @end
相关文章推荐
- iOS开发拓展篇—UIDynamic(重力行为+碰撞检测)
- 按钮点击切换背景图片(两种情况)
- UITableViewCell 选中时的颜色设置
- 转:UGUI与NGUI的区别与优缺点
- iOS开发拓展篇—UIDynamic(简单介绍)
- UIView绘制机制(转)
- Kaggle quick start
- 四核驱动的三维导航—淘宝新UI(设计篇)
- Perfect smooth scrolling in UITableViews
- UGUI技术之LayoutGroup布局实现详解
- Java Map获取key和value
- Android_Intent_passValueForResult
- UIView
- iOS7中计算UILabel中字符串的高度
- IOS UIView中间透明周围半透明(四种方法) 中间镂空
- UIView常用的一些方法setNeedsDisplay和setNeedsLayout
- ueditor .net配置
- 错误:Value '0000-00-00 00:00:00' can not be represented as java.sql.Timestamp;的解决
- 18.UISearchBar
- id to load is required for loading