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 ...
随机推荐
- 修改 SQL SERVER 2008 編輯前200筆 資料表問題? 转载自:http://www.dotblogs.com.tw/easy1201/archive/2008/12/04/6179.aspx
小弟前幾天 下載安裝了 SQL SERVER 2008 感覺系統效能還不錯 但是要編輯 資料表卻出現 很苦惱 但經過一番波折 終於了解如何改善 先執行SQL Server Management Stu ...
- log4j.xml常用配置
Log4J的配置文件(Configuration File)就是用来设置记录器的级别.存放器和布局的,它可接key=value格式的设置或xml格式的设置信息.通过配置,可以创建出Log4J的运行环境 ...
- yii安装redis扩展(Windows)
yii安装redis扩展可以用不同的方式, 最简单便捷的是使用 composer 方式, 有的时候composer会出现一些问题(现在还弄不懂),可能是网络什么的原因吧~ 还可以使用手动安装的方式, ...
- Appium Python测试环境搭建
详细参考地址:https://www.cnblogs.com/amoyshmily/p/10500687.html 1,Appium安装:https://github.com/appium/appiu ...
- Luogu P2824 [HEOI2016/TJOI2016]排序 线段树+脑子
只会两个$log$的$qwq$ 我们二分答案:设答案为$ans$,则我们把$a[i]<=ans$全部设成$0$,把$a[i]>ans$全部设成$1$,扔到线段树里,这样区间排序(升序)就是 ...
- Connect AS400 through firewall(JDBC will require ports: 449, 8470, 8471, and 8476)
What TCP ports are used by ODBC to connect to the DB2/400? 8471/9471 http://search400.techtarget.co ...
- 数据结构实验之链表八:Farey序列(SDUT 3331)
#include <bits/stdc++.h> using namespace std; typedef struct node { int data2; int data1;//mu ...
- Financial Management(SDUT 1007)
Problem Description Larry graduated this year and finally has a job. He's making a lot of money, but ...
- P2699 【数学1】小浩的幂次运算
原题链接 https://www.luogu.org/problemnew/show/P2699 P2699 [数学1]小浩的幂次运算 首先第一眼看这个题就知道要暴力枚举w^i 看是否在区间[l,r] ...
- Django基础之template
1. 模板系统的介绍 def current_datetime(request): now = datetime.datetime.now() html = "<html>< ...