iOS开发UIkit动力学UIDynamicAnimator一系列动画
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一系列动画的更多相关文章
- iOS开发笔记10:圆点缩放动画、强制更新、远程推送加语音提醒及UIView截屏
1.使用CAReplicatorLayer制作等待动画 CALayer+CABasicAnimation可以制作很多简单的动画效果,之前的博客中介绍的“两个动画”,一个是利用一张渐变色图片+CABas ...
- 【Swift】IOS开发中自定义转场动画
在IOS开发中,我们model另外一个控制器的时候,一般都使用默认的转场动画. 其实我们可以自定义一些转场动画.达到不同的转场效果. 步骤如下:(photoBrowser是目标控制器) 1.在源控制器 ...
- iOS开发UIKit框架-可视化编程-XIB
1. Interface Builder 可视化编程 1> 概述 GUI : 图形用户界面(Graphical User Interface, 简称GUI, 又称图形化界面) 是指采用图形方式显 ...
- ios开发之简单实现loading动画效果
最近有朋友问我类似微信语音播放的喇叭动画和界面图片加载loading界面是怎样实现的,是不是就是一个gif图片呢!我的回答当然是否定了,当然不排除也有人用gif图片啊!下面我就来罗列三种实现loadi ...
- iOS开发--QQ音乐练习,旋转动画的实现,音乐工具类的封装,定时器的使用技巧,SliderBar的事件处理
一.旋转动画的实现 二.音乐工具类的封装 -- 返回所有歌曲,返回当前播放歌曲,设置当前播放歌曲,返回下一首歌曲,返回上一首歌曲方法的实现 头文件 .m文件 #import "ChaosMu ...
- ios开发——实用技术篇&三维旋转动画
实现三位旋转动画的方法有很多种,这里介绍三种 一:UIView 1 [UIView animateWithDuration:1.0 animations:^{ 2 self.iconView.laye ...
- iOS开发之--UIImageView的animationImages动画
图片动画实现,代码如下: -(UIImageView *)animationImageView { if (!_animationImageView) { _animationImageView= [ ...
- IOS开发学习笔记022-imageView实现动画
这里要播放的动画是很多张连续的动画,连续播放就会显示出动画效果. 大概过程是: 新建一个single view application ,然后添加一个image View控件到视图.给image vi ...
- ios开发图层layer与核心动画二:CATransform3D,CAlayear和UIView区别,layer的position和anchorpoint
一:CATransform3D #import "ViewController.h" @interface ViewController () @property (weak, n ...
随机推荐
- 使用Tarjan进行缩点无向图
int From[maxn],Laxt[maxn],To[maxn<<2],Next[maxn<<2],cnt; int low[maxn],dfn[maxn],times,q ...
- Java&Selenium&JS&AWT之那些难以点击到的按钮
一.摘要 本篇博文的重点并不是简单的click()方法,而是要讲的是那些click()方法失效的时候的处理方式,其实做自动化久了我们都能发现研发的代码并不是都那么美丽,selenium支持的8种定位方 ...
- layui多图上传实现删除功能
在使用layui的多图上传时发现没有删除功能 在网上搜索解决办法时有的感觉太复杂有的不符合自己所需要的所以就自己动手 下面附上代码 HTML: <div class="layui-up ...
- MySQL--------SQL优化审核工具实战
1. 背景 SQLAdvisor是由美团点评公司技术工程部DBA团队(北京)开发维护的一个分析SQL给出索引优化建议的工具.它基于MySQL原生态词法解析,结合分析SQL中的where条件.聚合条件. ...
- HTML禁用一块区域点击
style="pointer-events: none;" 此方法可以禁止鼠标点击指定区域,但是对于键盘事件无法屏蔽,最好禁用一下键盘事件,如:tab
- CSS基础学习 18.CSS多列
四种常见的浏览器内核:
- FFmpeg常用命令学习笔记(四)处理原始数据命令
处理原始数据命令 通过音视频设备采集的.没有经过任何加工的数据叫原始数据,而像我们平时播放的比如mp4文件是压缩后的数据.视频原始数据是YUV格式,音频原始数据是PCM格式.FFmpeg可以从封装格 ...
- 《深入理解Java虚拟机》之(二、垃圾收集器与内存分配策略)
程序计数器.虚拟机栈.本地方法栈3个区域随线程而生,随线程而灭,这几个区域的内存分配和回收都具备确定性,不需要过多考虑回收的问题,因为方法结束或者线程结束时,内存自然就跟着回收了,而java堆和方法区 ...
- @GetMapping、@PostMapping、@PutMapping、@DeleteMapping、@PatchMapping、@RequestMapping详解
最近写项目中突然发现有人再controller层写@PostMapping,这对于经常用@RequestMapping的我来说,感到跟奇怪,网上搜寻了一些资料,特在此整合一下: Spring4.3中引 ...
- generator如何使用
把包安装好,然后配好,然后运行就可以了