Core Animation详解(三)-UIDynamic Animation
来源:互联网 发布:mql4编程手册 编辑:程序博客网 时间:2024/05/21 09:20
原创Blog,转载请注明出处
blog.csdn.net/hello_hwc
前言:本文主要包括以下几部分
- UIDynamic Animation是什么
- 如何创建UIDynamic Animation
- 每一种UIDynamic Animation讲解然后举例
- 附上Demo链接
由于都是动画,我就把这部分放在了Core Animation中了,严格意义上来说,它是Core Animation上层的封装。
一 什么是UIDynamic Animation
UIDynamic Animation是IOS 7引入的一个动态库,用来模拟现实世界的物理模型。隶属于UIKit,这里我仍然把它归到Core Animation的讲解之一。
主要模拟以下几种物理行为:
- UIGravityBehavior
- UIAttachmentBehavior
- UISnapBehavior
- UIPushBehavior
- UICollisionBehavior
- UIDynamicItemBehavior
二 如何创建一个UIDynamic Animation
(1)创建一个UIDynamicAnimator,大部分情况下只需要一个Animator
(2)创建一个或者多个UIDynamic Behavior,并添加到Animator
(3)为Dynamic Behavior添加Dynamic Item。
这里简单介绍下,什么是Dynamic Item。Dynamic Item是遵循UIDynamicItem Protocol的对象。UIView和UICollectionViewLayoutAttributes在IOS 7.0以后遵循了这个协议。所以可以直接使用。
三 UIGravityBehavior -重力
几个要配置的属性
magnitude - 重力加速度值 默认情况下1000points/second^2angle - 重力的方向(由弧度指定)gravityDirection - 重力的方向(由vector指定,默认(0.0,1.0))
举例
UIAttachmentBehavior * attachBeahavior = [[UIAttachmentBehavior alloc] initWithItem:self.imageview attachedToAnchor:CGPointMake(CGRectGetMidX(self.view.frame), 114)]; UIGravityBehavior * gravityBehavior = [[UIGravityBehavior alloc] initWithItems:@[self.imageview]]; [self.animator addBehavior:attachBeahavior]; [self.animator addBehavior:gravityBehavior];
四 UIDynamicItemBehavior
配置一些公用的属性,与其他的Dynamic Behavior共同配合
addAngularVelocity:forItem: - 添加角速度addLinearVelocity:forItem: - 添加线速度allowsRotation - 是否允许旋转angularResistance - 角速度方向的阻力density- 相对密度elasticity - 碰撞的弹性friction - 摩擦resistance-线性阻力
五 UICollisionBehavior - 碰撞
为Item与边界以及Item之间添加碰撞效果。
addBoundaryWithIdentifier:forPath: - 添加Path边界addBoundaryWithIdentifier:fromPoint:toPoint: - 添加线边界collisionMode - 碰撞模式(三种:只有Item之间;只有边界;边界以及Item之间都存在碰撞效果)translatesReferenceBoundsIntoBoundary - 把参考View的bounds转换为边界
举例
UIGravityBehavior * gravityBehavior = [[UIGravityBehavior alloc] init];// gravityBehavior.angle = M_PI/2; gravityBehavior.gravityDirection = CGVectorMake(0,1); gravityBehavior.magnitude = 0.5; [gravityBehavior addItem:self.imageview]; [self.animator addBehavior:gravityBehavior];
六 UISnapBehavior 这个不好翻译,附上文档描述
A snap behavior defines a dynamic item’s movement to a specified point; the movement proceeds with a spring-like effect, ending with an oscillation whose amount you can set.
配置属性:
damping - 动画结束的时候的震荡值 0.0-1.0;其中0.0最小,1.0最大。默认0.5
举例
UISnapBehavior * snapbehavior = [[UISnapBehavior alloc] initWithItem:self.imageview snapToPoint:self.view.center]; snapbehavior.damping = 0.65; [self.animator addBehavior:snapbehavior];
七 UIAttachmentBehavior - 吸附
为Items之间或者Item和锚点来创建吸附行为,如果是Item,默认吸附Item的中心,当然可以配置。
initWithItem:attachedToAnchor: - 初始化吸附到锚点
initWithItem:attachedToItem: - 初始化吸附到item
anchorPoint - 锚点
damping - 阻尼(阻力大小)
frequency - 震荡频率
length - 吸附的两个点之间的距离(锚点和Item或者Item之间)
举例
UIAttachmentBehavior * attachBeahavior = [[UIAttachmentBehavior alloc] initWithItem:self.imageview attachedToAnchor:CGPointMake(CGRectGetMidX(self.view.frame), 114)]; UIGravityBehavior * gravityBehavior = [[UIGravityBehavior alloc] initWithItems:@[self.imageview]]; [self.animator addBehavior:attachBeahavior]; [self.animator addBehavior:gravityBehavior];
八 UIPushBehavior - 推动
给一个Item持续或者瞬间的推力
initWithItems:mode: - 初始化并且指明是瞬间还是持续的推力
angle - 力的角度
magnitude - 推力大小
pushDirection - 推力方向
举例
UIPushBehavior * push = [[UIPushBehavior alloc] initWithItems:@[self.imageview] mode:UIPushBehaviorModeInstantaneous]; push.pushDirection = CGVectorMake(45, 0); push.magnitude = 1.0; UIDynamicItemBehavior * itemBehavior = [[UIDynamicItemBehavior alloc] initWithItems:@[self.imageview]]; itemBehavior.resistance = 0.8; [self.animator addBehavior:itemBehavior]; [self.animator addBehavior:push];
附上Demo 工程完整代码下载
http://download.csdn.net/detail/hello_hwc/8430701
BTY:对CSDN 的这个新的文本编辑器点赞
- Core Animation详解(三)-UIDynamic Animation
- Core Animation(三)动画详解
- Core Animation - 视觉效果<三>
- Core Animation - 变换<三>
- Core Animation 详解
- Core Animation Basics-Core Animation Programming Guide (1)
- Core Animation 三 (移动对象)
- iOS Core Animation详解(五)CATransition
- Core Animation(转)
- IOS动画Core Animation详解
- IOS动画Core Animation详解
- IOS动画Core Animation详解
- IOS动画Core Animation详解
- core animation calayer的详解
- IOS动画Core Animation详解
- IOS动画Core Animation详解
- IOS动画Core Animation详解
- IOS动画Core Animation详解
- jQuery获得客户端/浏览器的IP地址信息
- Linux Makefile多目录的编写
- Android 简单实现圆形ImageView添加双层圆形边框
- 学《何以》五招收服女友心
- 递归学习(一)--判断回文字符串
- Core Animation详解(三)-UIDynamic Animation
- 开源ECSHOP中SQL SELECT操作集锦,练习SQL
- 01背包问题和完全背包问题
- 数据结构(陈越)PAT练习题 第五周:图(上)
- 码农提高工作效率
- 微信订餐,订餐外卖系统功能 eatcms
- 排列数最后非零位:poj 1150 The Last Non-zero Digit
- MTK修改记事
- HDU 1556 Color the ball 线段树区间更新裸模板