Facebook POP 使用指南
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 查看更多的细节。
相关资源
以下是一些示例供你学习:
- AGGeometryKit+POP - Animating Quadrilaterals with Pop
- Apple – Core Animation Programming Guide
- Codeplease – Bridging the gesture to animation gap
- Codeplease – Playing with Pop (iii)
- Codeplease – Adding a custom animatable property
- iOS Development Tips – UIScrollView-like deceleration with Pop
- Pop Playground – Repository of Pop animation examples
- Pop Playground 2 – Playing with Facebook's framework
- POP-MCAnimate – Concise syntax for the Pop animation framework
- Popping - Great examples in one project
- Rebound – Spring Animations for Android
- Tapity Tutorial – Getting Started with Pop
- Tweaks – Easily adjust parameters for iOS apps in development
- POP Tutorial in 5 steps
- VBFPopFlatButton – Flat animatable button, using Pop to transition between states
Facebook POP 使用指南的更多相关文章
- Facebook POP 进阶指南
本文转自Kevin Blog Facebook 在发布了 Paper 之后,似乎还不满足于只是将其作为一个概念性产品,更进一步开源了其背后的动画引擎 POP,此举大有三年前发布的 iOS UI 框架 ...
- iOS开发Facebook POP动效库使用教程
如果说Origami这款动效原型工具是Facebook Paper的幕后功臣,那么POP便是Origami的地基.感谢Facebook开源了POP动效库,让人人都能制作出华丽的动效.我们只需5步,便能 ...
- Facebook POP动效库使用教程
编者注:用Origami作iOS动效的同学如果愁怎么实现,可以把这个给开发看看作为参考哦 如果说Origami这款动效原型工具是Facebook Paper的幕后功臣,那么POP便是Origami的地 ...
- 走进 Facebook POP 的世界
POP: 一个流行的可扩展的动画引擎iOS,它支持spring和衰变动态动画,使其可用于构建现实,基于物理交互.Objective - C API允许快速集成, 对于所有的动画和过渡他是成熟的. 解释 ...
- FaceBook pop 动画开源框架使用教程说明
https://github.com/facebook/pop Pop is an extensible animation engine for iOS and OS X. In addition ...
- faceBook Pop动画库手动添加版本
本人将pop的框架直接拖进工程里面然后按照教程导入头文件#import "POP.h"发现报找不到文件的错误,于是我手动将pop库里面所有类似于#import <POP/XX ...
- 阅读Facebook POP框架 笔记(一)
在这一系列文章里,我主要会将自己阅读第三方代码的经历记录下来,尝试独立分析解剖一个框架.之前也阅读过一些第三方代码,但是实际上来说对自己的成长并没有太大的帮助,因为阅读的不细致,没有领会到代码的精髓. ...
- iOS-常用的第三方框架的介绍
写iOS 程序的时候往往需要很多第三方框架的支持,可以大大减少工作量,讲重点放在软件本身的逻辑实现上. GitHub 里面有大量优秀的第三方框架,而且 License 对商业很友好.一下摘录一下几乎每 ...
- 我收录整理的优秀OC技术类文章
自定义导航按钮UIBarButtonItem 关于导航栏的六个小技巧 ios开发的一些小技巧篇一 制作一个可以滑动操作的 Table View Cell - IOS - 伯乐在线 一个 ...
随机推荐
- [心平气和读经典]The TCP/IP Guide(004)
The TCP/IP Guide [Page 44, 45, 46] Structure and Organization of The TCP/IP Guide | TCP/IP指南的组织结构 Yo ...
- grub2配置关键(三个核心变量prefix、root、cmdpath)和几点疑问
前置知识:你必须知道grub的启动过程以及bios和uefi的相关基础知识,可以参考:<Unified Extensible Firmware Interface Wikipedia>.& ...
- 去除inline-block之间的空白
做一个水平排列的导航通常有以下几种布局: 1.给一个浮动. 2.设置display为inline. 3.设置display为inline-block. 但要追求代码量最少的话,设置为inline元素或 ...
- 关于ActiveX在WebBrowser不加载问题
最近在做电子面单打印,需要在CS端集成web,这里我使用了WebBrowser,下文简称“wb”. wb可以简单的理解为IE的阉割版,它是支持ActiveX的,首先要确保ActiveX在IE中正常安装 ...
- git hub 建立公钥
1. 执行 $ eval "$(ssh-agent -s)" 2. 增加 ssh $ ssh-add ~/.ssh/id_rsa 3. 复制 生成的key (执行下面命令后就相当 ...
- JavaScript的原型链继承__propt__、prototype、constructor的理解、以及他们之间相互的关系。
回想自己已经工作了有一段时间了,但是自己对JavaScript的原型链.和继承的理解能力没有到位,最近他们彻底的整理并且复习了一遍. 本案例中部分文案来自网络和书籍,如有侵权请联系我,我只是把我的理解 ...
- Metronic 对话 chat
http://keenthemes.com/preview/metronic/theme/admin_1/index.html: jquery让滚动条默认在最底部:$('#content').scro ...
- 三、hdfs的JavaAPI操作
下文展示Java的API如何操作hdfs,在这之前你需要先安装配置好hdfs https://www.cnblogs.com/lay2017/p/9919905.html 依赖 你需要引入依赖如下 & ...
- Spring------约束导入和application.xml的引入方式
1.spring约束的导入 2.SSH常用约束 3.application.xml的引入方式 <1.通过ClassPathXmlApplicationContext引入配置文件applicati ...
- 撩课-Java每天5道面试题第12天
91.如何提升数据查询的效率? 1.首先检查表的结构是否合理, 因为采用多表查询的时候, 看主外键的引用关系是否适当. 如果不适当则重新设置表结构. 如果是应用中的系统, 则不需要更改表的字段, 只更 ...