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 - 伯乐在线 一个 ...
随机推荐
- hibernate关联关系的crud2
hibernate关联关系的CRUD操作,解释都在注释里了,讲了fetchType.cascade. User类: package com.oracle.hibernate; import javax ...
- 【html5】cookie、sessionStorage、localStorage
第四条补充: cookie中包含domain和path,所有向该域下该路径发送的请求头部都会包含这个cookie: session浏览器关闭后消失,只能由最初给对象存储数据的页面访 ...
- unity批量设置图片为etc2格式或者astc格式
网上找了半天,没一个能用的,干脆自己写个,直接拷贝这个脚本就行 这个是ios版本的,安卓的话写在注释里面,去掉注释就能用了 现在ios支持一种新格式叫astc比原本的pvrtc压缩比更高,而且质量更高 ...
- 创建第一个WCF服务
创建WCF服务 1. 新建立空白解决方案,并在解决方案中新建项目,项目类型为:WCF服务应用程序. 2.建立完成后如下图所示: 3.删除系统生成的两个文件IService1.cs与Service1.s ...
- Scrum 冲刺博客第七篇
一.当天站立式会议照片一张 二.每个人的工作 (有work item 的ID),并将其记录在码云项目管理中 昨天已完成的工作 对排行榜的界面和功能进行初步设计 今天计划完成的工作 重新对界面进行美化 ...
- java 继承多态的一些理解和不理解
1.向上转型的一个误区 一直以为Child 继承Parent以后, Parent p = new Child(); p可以调用Child类中拓展Parent的方法,原来必须在强制转换成Child类才 ...
- vue+webpack项目中使用dev-server搭建虚拟服务器,请求json文件数据,实现前后台分离开发
在项目开发中,前后台分离,做了假数据,项目使用vue2.0重构,后台也推到重来了,为了不耽误开发进程,我做了虚拟的数据请求,使用vue-cli脚手架搭建的项目文件中dev-server搭建虚拟api请 ...
- text-align真的只是让文本居中吗?
很多教程上说text-align属性只是让文本水平居中.但text-align的功能远不止如此. 对于具有文本类属性的元素,text-align属性也可以使其水平居中显示. 具有文本类属性的元素有:行 ...
- 【angular5项目积累总结】自定义管道 OrderBy
import { Injectable, Pipe } from '@angular/core'; @Pipe({ name: 'orderBy' }) @Injectable() export cl ...
- MYSQL登录错误:mysqladmin: connect to server at ‘localhost’ failed
一.mysql登录错误 mysqladmin: connect to server at 'localhost' failed error: 'Access denied for user ...