UIDynamicAnimator类,通过这个类中的不同行为来实现一些动态特性。

UIAttachmentBehavior(吸附),UICollisionBehavior(碰撞),UIGravityBehavior(重力),UIPushBehavior(推动),UISnapBehavior(捕捉)。另外还有一个辅助的行为UIDynamicItemBehavior,用来在item层级设定一些参数,比如item的摩擦,阻力,角阻力,弹性密度和可允许的旋转等等。

1.UIAttachmentBehavior(吸附):

- (void)viewDidLoad {
[super viewDidLoad]; self.dynamicAnimation = [[UIDynamicAnimator alloc]initWithReferenceView:self.view];
// 图片
self.imgView = [[UIImageView alloc]initWithFrame:CGRectMake(, , ,)];
self.imgView.image = [UIImage imageNamed:@"dizhijieyong_icon"];
[self.view addSubview:self.imgView];
self.imgView.userInteractionEnabled = YES; // 添加手势
UIPanGestureRecognizer *panGest = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGestAction:)];
[self.imgView addGestureRecognizer:panGest]; // 吸附 offset可以设置吸附的偏移,anchor是设置锚点
self.attachment = [[UIAttachmentBehavior alloc]initWithItem:self.imgView offsetFromCenter:UIOffsetMake(, ) attachedToAnchor:CGPointMake(, )];
} // 实现一个pan手势,让一个image跟着手势跑 - (void)panGestAction: (UIPanGestureRecognizer *)panGest{ NSLog(@"手势触发啦!!!"); // 取得location
CGPoint location = [panGest locationInView:self.view];
CGPoint boxLocation = [panGest locationInView:self.imgView]; switch (panGest.state) {
// 开始拖拽
case UIGestureRecognizerStateBegan: NSLog(@"you touch started position %@",NSStringFromCGPoint(location));
NSLog(@"location in image started is %@",NSStringFromCGPoint(boxLocation));
// 移除
[self.dynamicAnimation removeAllBehaviors]; UIOffset offSet = UIOffsetMake(boxLocation.x - CGRectGetMidX(self.imgView.bounds),
boxLocation.y - CGRectGetMidY(self.imgView.bounds)); self.attachment = [[UIAttachmentBehavior alloc]initWithItem:self.imgView offsetFromCenter:offSet attachedToAnchor:location];
self.attachment.damping=0.5;
self.attachment.frequency=0.8;
[self.dynamicAnimation addBehavior:self.attachment];
break;
case UIGestureRecognizerStateEnded:
[self.dynamicAnimation removeAllBehaviors];
default:
[self.attachment setAnchorPoint:location];
break;
}
}

2.UIGravityBehavior(重力):

// 实现点击屏幕掉落一张图片的代码

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    self.gravity = [[UIGravityBehavior alloc]initWithItems:nil];
[self.dynamicAnimation addBehavior:self.gravity]; UIImageView *gravImagView = [[UIImageView alloc]initWithFrame:CGRectMake(, , , )];
gravImagView.image = [UIImage imageNamed:@"zichanlingyong_icon"];
[self.view addSubview:gravImagView];
gravImagView.frame = CGRectMake(, , , );
[self.gravity addItem:gravImagView]; }

3.UISnapBehavior(捕捉):

// UISnapBehavior 将UIView通过动画吸附到某个点上
- (void) handleTap:(UITapGestureRecognizer *)paramTap{
CGPoint tapPoint = [paramTap locationInView:self.view]; if (self.snap != nil){
[self.dynamicAnimation removeBehavior:self.snap];
}
self.snap = [[UISnapBehavior alloc] initWithItem:self.imgView snapToPoint:tapPoint];
self.snap.damping = 0.5f; //剧列程度
[self.dynamicAnimation addBehavior:self.snap];
}

还有一些没有试过,有时间补充吧!!!嘻嘻~~~~~

iOS开发UIkit动力学UIDynamicAnimator一系列动画的更多相关文章

  1. iOS开发笔记10:圆点缩放动画、强制更新、远程推送加语音提醒及UIView截屏

    1.使用CAReplicatorLayer制作等待动画 CALayer+CABasicAnimation可以制作很多简单的动画效果,之前的博客中介绍的“两个动画”,一个是利用一张渐变色图片+CABas ...

  2. 【Swift】IOS开发中自定义转场动画

    在IOS开发中,我们model另外一个控制器的时候,一般都使用默认的转场动画. 其实我们可以自定义一些转场动画.达到不同的转场效果. 步骤如下:(photoBrowser是目标控制器) 1.在源控制器 ...

  3. iOS开发UIKit框架-可视化编程-XIB

    1. Interface Builder 可视化编程 1> 概述 GUI : 图形用户界面(Graphical User Interface, 简称GUI, 又称图形化界面) 是指采用图形方式显 ...

  4. ios开发之简单实现loading动画效果

    最近有朋友问我类似微信语音播放的喇叭动画和界面图片加载loading界面是怎样实现的,是不是就是一个gif图片呢!我的回答当然是否定了,当然不排除也有人用gif图片啊!下面我就来罗列三种实现loadi ...

  5. iOS开发--QQ音乐练习,旋转动画的实现,音乐工具类的封装,定时器的使用技巧,SliderBar的事件处理

    一.旋转动画的实现 二.音乐工具类的封装 -- 返回所有歌曲,返回当前播放歌曲,设置当前播放歌曲,返回下一首歌曲,返回上一首歌曲方法的实现 头文件 .m文件 #import "ChaosMu ...

  6. ios开发——实用技术篇&三维旋转动画

    实现三位旋转动画的方法有很多种,这里介绍三种 一:UIView 1 [UIView animateWithDuration:1.0 animations:^{ 2 self.iconView.laye ...

  7. iOS开发之--UIImageView的animationImages动画

    图片动画实现,代码如下: -(UIImageView *)animationImageView { if (!_animationImageView) { _animationImageView= [ ...

  8. IOS开发学习笔记022-imageView实现动画

    这里要播放的动画是很多张连续的动画,连续播放就会显示出动画效果. 大概过程是: 新建一个single view application ,然后添加一个image View控件到视图.给image vi ...

  9. ios开发图层layer与核心动画二:CATransform3D,CAlayear和UIView区别,layer的position和anchorpoint

    一:CATransform3D #import "ViewController.h" @interface ViewController () @property (weak, n ...

随机推荐

  1. IDEA设置CodeGlance颜色

    CodeGlance是IDEA的mini地图插件, 默认情况下, 其颜色和编辑框的颜色基本一致, 而安装CodeGlance就是为了方便滚动框的上下拖拉, 颜色一致的话会将这种CodeGlance比拖 ...

  2. P1052 过河 题解

    复习dp(迪皮)的时候刷到了一道简单路径压缩的题目(一点不会qwq) 题目描述链接. 正解: 首先呢,我们看到题目,自然而然的会想到这种思路: 设状态变量dp[i]表示从第一个格子开始经过一些跳跃跳到 ...

  3. hmmlearn 安装笔记

    hmmlearn是在python上实现隐马可夫模型的一个组件包,原先是在sklearn中的,后来被弃用而单独分离出来. 首先安装sklearn,最好下载setup.py的安装包用命令行安装,因为安装h ...

  4. [转]web.xml中servlet ,filter ,listener ,interceptor的作用与区别

    原文链接:https://blog.csdn.net/netdevgirl/article/details/51483273 一.概念: 1.servlet:servlet是一种运行服务器端的java ...

  5. java接口的多继承

    Java类之间并不允许多继承,只可以单继承和实现多接口,一直以为接口也是一样的,但是查阅了相关资料,突然豁然开朗. 一个类只能extends一个父类,但可以implements多个接口. 一个接口则可 ...

  6. MFC 标签页Tab Control

    自带的标签页不好用,因此借助了TabSheet文件TabSheet源码 1.在解决方案资源管理器——项目处鼠标右键——在文件资源管理器中打开文件夹(或者按下图,更方便),把TabSheet.h.Tab ...

  7. HGOI 20191103am 题解

    Problem A number 使用一个$2^k$数集中每个元素的和表示数$n$,不同集合的数目有多少? 对于$100\%$的数据满足$1 \leq n \leq 10^6$ Solution : ...

  8. OpenDayLight安装Features

    OpenDayLight 0.4.4-Beryllium-SR4 opendaylight-user@root>feature:install odl-restconf opendaylight ...

  9. 使用django uwsgi 导致磁盘满

    lsof |grep delete |sort -nrk 7|more kill 掉这些进程

  10. LeetCode109----链表转为二叉搜索树

    给定一个单链表,其中的元素按升序排序,将其转换为高度平衡的二叉搜索树. 本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1. 示例:给定的有序链表: [-10, ...