Facebook POP 使用指南

Pop是一个动画引擎,用以扩展iOS、OSX的动画类型。相较于iOS、OSX中的基本动画效果,Pop扩展后支持弹簧动画效果与衰减动画效果,你可以用Pop动画引擎来构建出真实的物理交互效果。它的API与Core Animation的API非常类似,使用起来非常容易。Pop动画引擎已经经过了良好的测试,我们在 Paper 应用中进行了大量使用。

安装

Pop支持 CocoaPods 安装,将下面一行代码添加到你的项目中的 Podfile 中:

    pod 'pop', '~> 1.0'

注意,bug会在主分支上面进行修复,然后在指定的分支上进行发布。如果你喜欢尝试最新的不大稳定的版本,你可以通过以下入口来访问主分支:

    pod 'pop', :git => 'https://github.com/facebook/pop.git'

使用

Pop 支持Core Animation 中的显式动画类型,你可以通过导入头文件来使用它:

    #import <pop/POP.h>

开始动画、停止动画与更新动画

开始执行一个动画,你可以将动画添加到一个对象中:

POPSpringAnimation *anim = [POPSpringAnimation animation];
...
[layer pop_addAnimation:anim forKey:@"myKey"];

停止一个动画,你可以根据一个键值来从对象中移除掉:

[layer pop_removeAnimationForKey:@"myKey"];

你也可以根据键值来查询已经存在的动画,你可以在执行动画效果的同时来修改toValue属性来实时更新动画效果:

anim = [layer pop_animationForKey:@"myKey"];
if (anim) {
/* update to value to new destination */
anim.toValue = @(42.0);
} else {
/* create and start a new animation */
....
}

注意,虽然上述示例中用到了一个layer,但是Pop动画引擎是基于 NSObject 所写的一个category,任何继承自NSObject的对象都可以使用Pop动画引擎。

动画类型

Pop支持4种动画类型:弹簧效果、衰减效果、基本动画效果与自定义动画效果。

弹簧效果可以用来实现仿真的物理弹簧特效,在下面的这个例子中,我们用弹簧效果来对一个layer的尺寸进行缩放:

效果图:

源码:

#import "ViewController.h"
#import "POP.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; // 创建layer
CALayer *layer = [CALayer layer];
layer.frame = CGRectMake(0, 0, 50, 50);
layer.backgroundColor = [UIColor cyanColor].CGColor;
layer.cornerRadius = 25.f;
layer.position = self.view.center;
[self.view.layer addSublayer:layer]; // 执行Spring动画
POPSpringAnimation *anim = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerScaleXY];
anim.toValue = [NSValue valueWithCGPoint:CGPointMake(3.f, 3.f)];
anim.springSpeed = 0.f;
[layer pop_addAnimation:anim forKey:@"ScaleXY"];
} @end

衰减效果可以用来模拟真实的物理减速效果,在下面的例子中,我们用衰减效果来对一个view的拖拽停止执行减速效果。

效果图:

源码:

#import "ViewController.h"
#import "POP.h" @interface ViewController ()<POPAnimationDelegate> @property(nonatomic) UIControl *dragView; @end @implementation ViewController - (void)viewDidLoad
{
[super viewDidLoad]; // 初始化dragView
self.dragView = [[UIControl alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
self.dragView.center = self.view.center;
self.dragView.layer.cornerRadius = CGRectGetWidth(self.dragView.bounds)/2;
self.dragView.backgroundColor = [UIColor cyanColor];
[self.view addSubview:self.dragView];
[self.dragView addTarget:self
action:@selector(touchDown:)
forControlEvents:UIControlEventTouchDown]; // 添加手势
UIPanGestureRecognizer *recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self
action:@selector(handlePan:)];
[self.dragView addGestureRecognizer:recognizer];
} - (void)touchDown:(UIControl *)sender {
[sender.layer pop_removeAllAnimations];
} - (void)handlePan:(UIPanGestureRecognizer *)recognizer {
// 拖拽
CGPoint translation = [recognizer translationInView:self.view];
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
recognizer.view.center.y + translation.y);
[recognizer setTranslation:CGPointMake(0, 0) inView:self.view]; // 拖拽动作结束
if(recognizer.state == UIGestureRecognizerStateEnded)
{
// 计算出移动的速度
CGPoint velocity = [recognizer velocityInView:self.view]; // 衰退减速动画
POPDecayAnimation *positionAnimation = \
[POPDecayAnimation animationWithPropertyNamed:kPOPLayerPosition]; // 设置代理
positionAnimation.delegate = self; // 设置速度动画
positionAnimation.velocity = [NSValue valueWithCGPoint:velocity]; // 添加动画
[recognizer.view.layer pop_addAnimation:positionAnimation
forKey:@"layerPositionAnimation"];
}
} @end

基本动画效果可以指定具体的动画时间,与 CoreAnimation 中的 CABasicAnimation 的用法极为类似,在下面的例子中,我们用基本动画效果来设置一个view的alpha值。

效果图:

源码:

#import "ViewController.h"
#import "POP.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad
{
[super viewDidLoad]; // 创建view
UIView *showView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
showView.alpha = 0.f;
showView.layer.cornerRadius = 50.f;
showView.center = self.view.center;
showView.backgroundColor = [UIColor cyanColor];
[self.view addSubview:showView]; // 执行基本动画效果
POPBasicAnimation *anim = [POPBasicAnimation animationWithPropertyNamed:kPOPViewAlpha];
anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
anim.fromValue = @(0.0);
anim.toValue = @(1.0);
anim.duration = 4.f;
[showView pop_addAnimation:anim forKey:@"fade"];
} @end

自定义动画效果是根据 CADisplayLink 来计算出中间的差值,然后由你来处理输出的值的动画效果,详情请参考相关头文件来获取更多的细节。

属性

属性是通过 POPAnimatableProperty 来定义的。在下面的这个例子中,我们创建出了一个弹簧动画效果并显式的设置它去响应 -[CALayer bounds]

POPSpringAnimation *anim = [POPSpringAnimation animation];
anim.property = [POPAnimatableProperty propertyWithName:kPOPLayerBounds];

Pop动画引擎本身提供了很多可以做动画的属性供你定制。你可以在这个类里面创建出一个实例对象:

prop = [POPAnimatableProperty propertyWithName:@"com.foo.radio.volume" initializer:^(POPMutableAnimatableProperty *prop) {
// read value
prop.readBlock = ^(id obj, CGFloat values[]) {
values[0] = [obj volume];
};
// write value
prop.writeBlock = ^(id obj, const CGFloat values[]) {
[obj setVolume:values[0]];
};
// dynamics threshold
prop.threshold = 0.01;
}]; anim.property = prop;

为了了解更多的可以做动画效果的属性,你可以参考 POPAnimatableProperty.h 查看更多的细节。

相关资源

以下是一些示例供你学习:

Facebook POP 使用指南的更多相关文章

  1. Facebook POP 进阶指南

    本文转自Kevin Blog Facebook 在发布了 Paper 之后,似乎还不满足于只是将其作为一个概念性产品,更进一步开源了其背后的动画引擎 POP,此举大有三年前发布的 iOS UI 框架  ...

  2. iOS开发Facebook POP动效库使用教程

    如果说Origami这款动效原型工具是Facebook Paper的幕后功臣,那么POP便是Origami的地基.感谢Facebook开源了POP动效库,让人人都能制作出华丽的动效.我们只需5步,便能 ...

  3. Facebook POP动效库使用教程

    编者注:用Origami作iOS动效的同学如果愁怎么实现,可以把这个给开发看看作为参考哦 如果说Origami这款动效原型工具是Facebook Paper的幕后功臣,那么POP便是Origami的地 ...

  4. 走进 Facebook POP 的世界

    POP: 一个流行的可扩展的动画引擎iOS,它支持spring和衰变动态动画,使其可用于构建现实,基于物理交互.Objective - C API允许快速集成, 对于所有的动画和过渡他是成熟的. 解释 ...

  5. FaceBook pop 动画开源框架使用教程说明

    https://github.com/facebook/pop Pop is an extensible animation engine for iOS and OS X. In addition ...

  6. faceBook Pop动画库手动添加版本

    本人将pop的框架直接拖进工程里面然后按照教程导入头文件#import "POP.h"发现报找不到文件的错误,于是我手动将pop库里面所有类似于#import <POP/XX ...

  7. 阅读Facebook POP框架 笔记(一)

    在这一系列文章里,我主要会将自己阅读第三方代码的经历记录下来,尝试独立分析解剖一个框架.之前也阅读过一些第三方代码,但是实际上来说对自己的成长并没有太大的帮助,因为阅读的不细致,没有领会到代码的精髓. ...

  8. iOS-常用的第三方框架的介绍

    写iOS 程序的时候往往需要很多第三方框架的支持,可以大大减少工作量,讲重点放在软件本身的逻辑实现上. GitHub 里面有大量优秀的第三方框架,而且 License 对商业很友好.一下摘录一下几乎每 ...

  9. 我收录整理的优秀OC技术类文章

        自定义导航按钮UIBarButtonItem   关于导航栏的六个小技巧   ios开发的一些小技巧篇一 制作一个可以滑动操作的 Table View Cell - IOS - 伯乐在线 一个 ...

随机推荐

  1. Window.localStorage

    博客园 https://www.cnblogs.com/st-leslie/p/5617130.html 参考文档 https://developer.mozilla.org/zh-CN/docs/W ...

  2. JavaScript设计模式-17.装饰者模式(下)

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  3. python笔记08-----正则表达式

    创建正则表达式对象 import re 常用匹配语法 re.match 从头开始匹配 re.search 匹配包含 re.findall 把所有匹配到的字符放到以列表中的元素返回 re.splital ...

  4. [C语言]日期间天数差值的计算

    刷一些算法题时总能遇到计算日期间天数的问题,每每遇到这种情况,不是打开excel就是用系统自带的计算器.私以为这种问题及其简单以至于不需要自己动脑子,只要会调用工具就好.直到近些天在写一个日历程序的时 ...

  5. Comet:基于 HTTP 长连接的“服务器推”技术(转载)

    “服务器推”技术的应用 传统模式的 Web 系统以客户端发出请求.服务器端响应的方式工作.这种方式并不能满足很多现实应用的需求,譬如: 监控系统:后台硬件热插拔.LED.温度.电压发生变化: 即时通信 ...

  6. java当中的定时器

    对于开发游戏项目的同胞来说,Timer 这个东西肯定不会陌生,今天对以前自己经常使用的定时进行了一番小小的总结!没有写具体实现的原理,只是列举出了其中的四种比较常见的使用方法,相对而言,所以只要按照其 ...

  7. Numpy API

    Numpy API 矩阵操作 np.squeeze(mat): 将mat降维 np.linalg.norm(x, axis=1, keepdims=True): keepdim=True是防止出现sh ...

  8. Angular2 不明真相第一个Demo例子

    如果不是去年换工作接触到AngularJS,估计是不会花时间去学习这个框架的,毕竟是前端的框架,不是自己熟悉的领域.但是为了混得下去,去年就学习了AngularJS的一些用法,当时还整理了一些积累 & ...

  9. Form身份验证

    Forms身份验证Web.config<system.web><authentication mode="Forms">            <fo ...

  10. spring与dwr整合实现js直接使用java代码

    此文章是基于 搭建Jquery+SpringMVC+Spring+Hibernate+MySQL平台 一. jar包介绍 1. dwr-3.0.1.jar,支持 spring 4.3.4 的最低版本 ...