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. iView - DatePicker组件神坑,如何处理?

    最近使用iView - DatePicker组件时发现一些问题,明明设置是正常的日期时间格式,当需要使用这个时间的时候,页面却显示 Fri Jun 09 2017 12:00:10 GMT+0800 ...

  2. 一段有关线搜索的从python到matlab的代码

    在Udacity上很多关于机器学习的课程几乎都是基于python语言的,博主“ttang”的博文“重新发现梯度下降法——backtracking line search”里对回溯线搜索的算法实现也是用 ...

  3. Gym - 101981B Tournament (WQS二分+单调性优化dp)

    题意:x轴上有n个人,让你放置m个集合点,使得每个人往离他最近的集合点走,所有人走的距离和最短. 把距离视为花费,设$dp[i][k]$表示前i个人分成k段的最小花费,则有递推式$dp[i][k]=m ...

  4. POJ1423 Big Number 暴力or斯特林公式??

    好吧这题很水...可是我没想到正解... 题意:求n!有多少位. 正解:斯特林公式. 直接放代码... #include<cstdio> #include<iostream> ...

  5. .net core Redis库 CSRedis

    由于servicestack.redis收费,基于有人说StackExchange.Redis 使用会出现一些问题比如会超时, 找到了CSRedis这个库,很强大很实用.另外有兴趣的朋友还可以试试另一 ...

  6. ARTS打卡计划第十二周

    Algorithms: https://leetcode-cn.com/problems/balanced-binary-tree/ 平衡二叉树. Review:  “What I Learned i ...

  7. python获得坐标系信息

    # -*- coding: cp936 -*- ############################################################# import arcpy i ...

  8. Process.Net

    ProcessSharp的构造函数,对应的测试是 https://github.com/lolp1/Process.NET/blob/master/test/Process.NET.Test/Core ...

  9. ReSharper “Cannot resolve symbol” even when project builds

    ReSharper “Cannot resolve symbol” even when project builds   This worked for me (VS2012u4, R# 7.1.3) ...

  10. 编译安装 Nginx

    一.下载 https://nginx.org/en/download.html yum install -y wget wget http://nginx.org/download/nginx-1.1 ...