UIKit Dynamics 类型整理和代码归类

1, 掉落(重力)行为--UIGravityBehavior

2,碰撞行为--UICollisionBehavior

3,抛掷行为--UIPushBehavior

4,吸附行为--UIAttachmentBehavior

5,迅猛移动弹跳摆动行为 --UISnapBehavior

6,动力元素行为--UIDynamicItemBehavior



一 ,掉落(重力)行为--UIGravityBehavior

常用方法:

- (void)addItem:(id)item;

- (void)removeItem:(id)item;

- (void)setAngle:(CGFloat)angle magnitude:(CGFloat)magnitude;

添加或者删除对象组

常用属性:

CGVector gravityDirection;重力方向(是一个二维向量)默认值 (0,1),代数几何中的方向向量,x,y所成角度即是重力掉落方向

CGFloat angle;重力方向(是一个角度,以x轴正方向为0°,顺时针正数,逆时针负数)

-360-360

CGFloat magnitude;量级(用来控制加速度,1.0代表加速度是1000 points /second²)

具体使用是这样的:gravityDirection 二维向量确定向量的方向,在确定方向后,根据angle 的 大小决定下落方向,magnitude决定下落的加速度大小。


.h文件

@property (nonatomic, strong) UIView *squareView;

@property (nonatomic, strong) UIDynamicAnimator *animator;


.m文件

/*重力效果*/

self.squareView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];

self.squareView.backgroundColor = [UIColor yellowColor];

self.squareView.center = self.view.center;

[self.view addSubview:self.squareView];

UIGravityBehavior *gravity = [[UIGravityBehavior alloc] initWithItems:@[self.squareView]];

gravity.gravityDirection = CGVectorMake(0, 1);

gravity.angle = 45;

gravity.magnitude = 5.0;

self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];

[self.animator addBehavior:gravity];



二,碰撞行为--UICollisionBehavior

常用的方法

- (instancetype)initWithItems:(NSArray *)items;

- (void)addItem:(id)item;

- (void)removeItem:(id)item;

@property (nonatomic, readonly, copy) NSArray* items;

- (void)removeAllBoundaries;

参数分析:

UICollisionBehaviorMode collisionMode;

有以下三个参数

UICollisionBehaviorModeItems        = 1 << 0,

   设置这个参数说明碰撞会在dynamic items之间检测

UICollisionBehaviorModeBoundaries  = 1 << 1,

  设置这个参数说明碰撞在我们设置的边界上检测

UICollisionBehaviorModeEverything  = NSUIntegerMax

  设置这个参数说明检测一切的碰撞,不考虑items,边界,或者其他


这三个值可以使用 | 组合使用


BOOL translatesReferenceBoundsIntoBoundary;是否以参照视图的bounds为边界

- (void)setTranslatesReferenceBoundsIntoBoundaryWithInsets:(UIEdgeInsets)insets; 与上一个参数一致,设置EdgeInsets

- (void)addBoundaryWithIdentifier:(id)identifier forPath:(UIBezierPath*)bezierPath;

- (void)addBoundaryWithIdentifier:(id)identifier fromPoint:(CGPoint)p1 toPoint:(CGPoint)p2;

- (UIBezierPath*)boundaryWithIdentifier:(id)identifier;

- (void)removeBoundaryWithIdentifier:(id)identifier;

自定义路线,要么使用 UIBezierPath,要么自定义point

@property (nonatomic, assign, readwrite) id collisionDelegate;

设置Contact不同状态下需要自定义的行为,


代码如下:

.h文件

@property (nonatomic, strong) NSMutableArray *squareViews;

@property (nonatomic, strong) UIDynamicAnimator *animator;


.m文件

- (void) viewDidAppear:(BOOL)animated-内

/* 碰撞效果 */

self.squareViews = [[NSMutableArray alloc] initWithCapacity:5];

NSArray *colors = @[[UIColor greenColor],[UIColor yellowColor]];

CGPoint currentCenterPoint = self.view.center;

CGSize eachViewSize = CGSizeMake(50, 50);

for (int counter = 0; counter < 2; counter ++) {

UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, eachViewSize.width, eachViewSize.height)];

newView.backgroundColor = colors[counter];

newView.center = currentCenterPoint;

currentCenterPoint.y += eachViewSize.height +10;

[self.view addSubview:newView];

[self.squareViews addObject:newView];

}

self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];

/*create gravity*/

UIGravityBehavior *gravity = [[UIGravityBehavior alloc] initWithItems:self.squareViews];

[self.animator addBehavior:gravity];

/* create collision */

UICollisionBehavior *collision = [[UICollisionBehavior alloc] initWithItems:self.squareViews];

//    collision.translatesReferenceBoundsIntoBoundary = YES;

collision.collisionMode = UICollisionBehaviorModeBoundaries|UICollisionBehaviorModeItems|UICollisionBehaviorModeEverything;

collision.collisionDelegate = self;

[collision

addBoundaryWithIdentifier:kBottomIdentifier

fromPoint:CGPointMake(0.0f, self.view.bounds.size.height - 100.0f)

toPoint:CGPointMake(self.view.bounds.size.width,

self.view.bounds.size.height - 100.0f)];

[self.animator addBehavior:collision];


#pragma mark UICollisionViewDelegate 

- (void)collisionBehavior:(UICollisionBehavior *)behavior beganContactForItem:(id)item withBoundaryIdentifier:(id)identifier atPoint:(CGPoint)p

{

NSString *paramIdentifier = (NSString *)identifier;

if ([paramIdentifier isEqualToString:kBottomIdentifier]) {

[UIView animateWithDuration:0.5 animations:^{

UIView *view = (UIView *)item;

view.backgroundColor = [UIColor redColor];

view.alpha = 0.0;

view.transform = CGAffineTransformMakeScale(2.0f, 2.0f);

} completion:^(BOOL finished) {

UIView *view = (UIView *)item;

[behavior removeItem:item];

[view removeFromSuperview];

}];

}

}

三,抛掷行为--UIPushBehavior

常用方法:

- (instancetype)initWithItems:(NSArray *)items mode:(UIPushBehaviorMode)mode;

- (void)addItem:(id)item;

- (void)removeItem:(id)item;

@property (nonatomic, readonly, copy) NSArray* items;

常用参数:

UIPushBehaviorMode mode;

下面有两个参数:

UIPushBehaviorModeContinuous,

UIPushBehaviorModeInstantaneous


GFloat angle;

CGFloat magnitude;(1.0  = 100 points per s^2)

CGVector pushDirection;向量方向 和 UIGravityBehavior类似



4,吸附行为--UIAttachmentBehavior

attachment是里面相对复杂的一个,不过也是不错的一个行为

属性:

CGPoint anchorPoint;  锚点

CGFloat length;吸附行为中的两个吸附点之间的距离,通常用这个属性来调整吸附的长度

CGFloat damping;描述吸附行为减弱的阻力大小

CGFloat frequency;吸附行为震荡的频率

UIAttachmentBehaviorType attachedBehaviorType;吸附类型,有下面两个值

UIAttachmentBehaviorTypeItems,表示连接两个item的吸附行为

UIAttachmentBehaviorTypeAnchor,表示连接一个item与锚点的吸附行为


附代码

.h文件

@property (nonatomic, strong) UIView *squareView;

@property (nonatomic, strong) UIView *squareViewAnchorView;

@property (nonatomic, strong) UIView *anchorView;

@property (nonatomic, strong) UIAttachmentBehavior *atachmentBehavior;

@property (nonatomic, strong) UIDynamicAnimator *animator;


.m文件

/*吸附动画*/

- (void) viewDidAppear:(BOOL)animated

{

[super viewDidAppear:animated];

[self createSmallSquareView];

[self createAnchorView];

[self createGestureRecgnizer];

[self creataAnimatorAndBehavior];

}

/* attachmentBehavoir */

- (void) createSmallSquareView

{

self.squareView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];

self.squareView.backgroundColor = [UIColor greenColor];

self.squareView.center = self.view.center;

self.squareViewAnchorView = [[UIView alloc] initWithFrame:CGRectMake(60, 0, 20, 20)];

self.squareViewAnchorView.backgroundColor = [UIColor brownColor];

[self.squareView addSubview:self.squareViewAnchorView];

[self.view addSubview:self.squareView];

}

- (void) createAnchorView

{

self.anchorView = [[UIView alloc] initWithFrame:CGRectMake(120, 120, 20, 20)];

self.anchorView.backgroundColor = [UIColor redColor];

[self.view addSubview:self.anchorView];

}

- (void) createGestureRecgnizer

{

UIPanGestureRecognizer *panGestureRecgnizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];

[self.view addGestureRecognizer:panGestureRecgnizer];

}

- (void) creataAnimatorAndBehavior

{

self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];

/* create collision detection*/

UICollisionBehavior *collision = [[UICollisionBehavior alloc] initWithItems:@[self.squareView]];

collision.translatesReferenceBoundsIntoBoundary = YES;

self.atachmentBehavior = [[UIAttachmentBehavior alloc] initWithItem:self.squareView offsetFromCenter:UIOffsetMake(self.squareViewAnchorView.center.x,self.squareViewAnchorView.center.y) attachedToAnchor:self.anchorView.center];

self.atachmentBehavior.length = 100;

self.atachmentBehavior.frequency = 50;

self.atachmentBehavior.damping = 3.0;

[self.animator addBehavior:collision];

[self.animator addBehavior:self.atachmentBehavior];

}

- (void) handlePan:(UIPanGestureRecognizer *)panGesture

{

CGPoint tapPoint = [panGesture locationInView:self.view];

[self.atachmentBehavior setAnchorPoint:tapPoint];

self.anchorView.center = tapPoint;

}


5,迅猛移动弹跳摆动行为 --UISnapBehavior

其本身的效果,类似于果冻在光滑的桌面滑动的效果,挺不错的,萌萌搭。

这个和上面的没什么不同,区分点在于,在更新snapBehavior 时,要先移除旧的 snapBehavior,每一个animator 只能保有一个snapBehavior ,当加入多个的时候,animator 无法区分你要做哪个,索性就不做了。

属性:

CGFloat damping;就是滑动到point时晃动的剧烈程度,值从0到1


代码实例

.h文件

@property (nonatomic, strong) UISnapBehavior *snapBehavoir;

@property (nonatomic, strong) UIDynamicAnimator *animator;


.m文件

- (void) viewDidAppear:(BOOL)animated

{

[super viewDidAppear:animated];

/* UISnapBehavior */

[self createSmallSquareView];

[self createGestureRecgnizer];

[self createAnimatorAndBehavoir];

/*UISnapBehavoir */

- (void) createSmallSquareView

{

self.squareView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];

self.squareView.backgroundColor = [UIColor greenColor];

self.squareView.center = self.view.center;

[self.view addSubview:self.squareView];

}

- (void) createAnimatorAndBehavoir

{

self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];

/* create collision detection */

UICollisionBehavior *collisionBehavier = [[UICollisionBehavior alloc] initWithItems:@[self.squareView]];

collisionBehavier.translatesReferenceBoundsIntoBoundary = YES;

[self.animator addBehavior:collisionBehavier];

self.snapBehavoir = [[UISnapBehavior alloc] initWithItem:self.squareView snapToPoint:self.squareView.center];

self.snapBehavoir.damping = 0.5f;

[self.animator addBehavior:self.snapBehavoir];

}

- (void) createGestureRecgnizer

{

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];

[self.view addGestureRecognizer:tap];

}

- (void) tapAction:(UITapGestureRecognizer *)tapGesture

{

CGPoint tapPoint = [tapGesture locationInView:self.view];

if (self.snapBehavoir != nil) {

[self.animator removeBehavior:self.snapBehavoir];

}

self.snapBehavoir = [[UISnapBehavior alloc] initWithItem:self.squareView

snapToPoint:tapPoint];

self.snapBehavoir.damping = 0.5f; /* Medium oscillation */

[self.animator addBehavior:self.snapBehavoir];

}


六,动力元素行为--UIDynamicItemBehavior

真实物体特效,可以配合弥补前面的一些不足

BOOL allowsRotation; 强制某物不旋转

CGFloat angularResistance;

CGFloat friction;0-1,决定了这个物质与其他物质擦边或者碰撞时的摩擦力。

CGFloat resistance;运动阻力,大小0-CGFLOAT_MAX

CGFloat elasticity;0-1说明其弹性大小

CGFloat density;不会直接使用,被animator用来计算这如何影响你的动画。

代码示例:

.m文件

- (void) viewDidAppear:(BOOL)animated

{

[super viewDidAppear:animated];

UIView *topView = [self newViewWithCenter:CGPointMake(100, 0) backgroundColor:[UIColor greenColor]];

UIView * bottomView = [self newViewWithCenter:CGPointMake(100, 50) backgroundColor:[UIColor yellowColor]];

[self.view addSubview:topView];

[self.view addSubview:bottomView];

self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];

/*create gravity*/

UIGravityBehavior *gravity = [[UIGravityBehavior alloc] initWithItems:@[topView,bottomView]];

[self.animator addBehavior:gravity];

/*create collision */

UICollisionBehavior *collision = [[UICollisionBehavior alloc] initWithItems:@[topView, bottomView]];;

collision.translatesReferenceBoundsIntoBoundary = YES;

[self.animator addBehavior:collision];

/*create DynamicItem */

UIDynamicItemBehavior *moreElasticItem = [[UIDynamicItemBehavior alloc] initWithItems:@[bottomView]];

moreElasticItem.elasticity = 1.0f;

UIDynamicItemBehavior *lessElasticItem = [[UIDynamicItemBehavior alloc] initWithItems:@[topView]];

lessElasticItem.elasticity = 0.5f;

[self.animator addBehavior:moreElasticItem];

[self.animator addBehavior:lessElasticItem];


- (UIView *) newViewWithCenter:(CGPoint)parament

backgroundColor:(UIColor *)paramBackground

{

UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];

newView.backgroundColor = paramBackground;

newView.center = parament;

return newView;

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,271评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,275评论 2 380
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,151评论 0 336
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,550评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,553评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,559评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,924评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,580评论 0 257
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,826评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,578评论 2 320
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,661评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,363评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,940评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,926评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,156评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,872评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,391评论 2 342

推荐阅读更多精彩内容