[翻译] POP Facebook的动画开源库

Pop is an extensible animation engine for iOS and OS X. In addition to basic static animations, it supports spring and decay dynamic animations, making it useful for building realistic, physics-based interactions. The API allows quick integration with existing Objective-C codebases and enables the animation of any property on any object. It's a mature and well-tested framework that drives all the animations and transitions in Paper.

Pop是一个对iOS以及OS X扩展的动画引擎。除了基础静态的动画,它支持弹簧类以及衰退的物理引擎效果的动画。对于你来创建出类似于物理引擎的交互十分方便。这个API能让你快速的集成到你已经存在的项目当中,允许你对任何对象的任何属性操作。他是个系统的,进行过反复测试的框架,在Paper应用中所有的动画均由Pop创建。

 

Installation

Pop is available on CocoaPods. Just add the following to your project Podfile:

你可以使用CocoaPods安装:

pod 'pop', '~> 1.0'

Alternatively, you can add the project to your workspace and adopt the provided configuration files or manually copy the files under the pop subdirectory into your project. If installing manually, ensure the C++ standard library is also linked by including -lc++ to your project linker flags.

另一种方式是:你可以将整个工程添加到你的workspace中然后引用需要的配置文件,或者是手动的复制pop文件夹到你的工程项目当中,确保你的项目中用 -lc++ 链接到C++标准库。

 

Usage

Pop adopts the Core Animation explicit animation programming model. Use by including the following import:

Pop 采用了 Core Animation 明确的动画程勋模型,通过引入头文件来使用:

#import <POP/POP.h>

Start, Stop & Update

To start an animation, add it to the object you wish to animate:

为了给对象添加动画效果,使用如下:

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

To stop an animation, remove it from the object referencing the key specified on start:

为了停止动画,你可以从指定的key值,从对象移除掉:

[layer pop_removeAnimationForKey:@"myKey"];

The key can also be used to query for the existence of an animation. Updating the toValue of a running animation can provide the most seamless way to change course:

这个key值也可以用来查询已经存在的动画。更新 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 */
  ....
}

While a layer was used in the above examples, the Pop interface is implemented as a category addition on NSObject. Any NSObject or subclass can be animated.

上例中使用了一个layer,然而,Pop是通过对NSObject的category实现方式。任何的NSObject或者其只对象都能够使用动画。

 

Types

There are four concrete animation types: spring, decay, basic and custom.

有着4种具体的动画类型:弹簧类型,衰退的物理引擎类型,基础类型以及定制类型。

Spring animations can be used to give objects a delightful bounce. In this example, we use a spring animation to animate a layer's bounds from its current value to (0, 0, 400, 400):

弹簧动画效果可以给对象一个很平滑的动画效果。使用如下例子所示:

POPSpringAnimation *anim = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerBounds];
anim.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 400, 400)];
[layer pop_addAnimation:anim forKey:@"size"];

Decay animations can be used to gradually slow an object to a halt. In this example, we decay a layer's positionX from it's current value and velocity 1000pts per second:

类似于物理引擎的动画可以慢慢将一个对象从运动状态变为停止状态。使用如下例子所示:

POPDecayAnimation *anim = [POPDecayAnimation animationWithPropertyNamed:kPOPLayerPositionX];
anim.velocity = @(1000.);
[layer pop_addAnimation:anim forKey:@"slide"];

Basic animations can be used to interpolate values over a specified time period. To use an ease-in ease-out animation to animate a view's alpha from 0.0 to 1.0 over the default duration:

基本动画类型可以用来篡改一个指定的时间区域。使用如下例子:

POPBasicAnimation *anim = [POPBasicAnimation animationWithPropertyNamed:kPOPViewAlpha];
anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
anim.fromValue = @(0.0);
anim.toValue = @(1.0);
[view pop_addAnimation:anim forKey:@"fade"];

POPCustomAnimation makes creating custom animations and transitions easier by handling CADisplayLink and associated time-step management. See header for more details.

POPCustomAnimation 通过控制CADisplayLink以及与时间轴的管理,使得创建自定义动画以及转场更加的简单,你可以在头文件中浏览。

Properties

The property animated is specified by the POPAnimatableProperty class. In this example we create a spring animation and explicitly set the animatable property corresponding to -[CALayer bounds]:

用来触发动画的类是POPAnimatableProperty。这个例子中,我们创建了一个弹簧动画,以及明确的设定了这个动画来响应-[CALayer bounds]:

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

The framework provides many common layer and view animatable properties out of box. You can animate a custom property by creating a new instance of the class. In this example, we declare a custom volume property:

这个框架提供了很多种通用的layer以及可以被动画的view的属性。你可以通过创建一个实例来自定义一个可以动画的属性。以下例子提供了如何让音量属性动画改变:

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;

For a complete listing of provided animatable properties, as well more information on declaring custom properties see POPAnimatableProperty.h.

请参考POPAnimatableProperty.h来获取一份完整的可以提供动画完整的属性列表。

 

Debugging

Here are a few tips when debugging. Pop obeys the Simulator's Toggle Slow Animations setting. Try enabling it to slow down animations and more easily observe interactions.

Consider naming your animations. This will allow you to more easily identify them when referencing them, either via logging or in the debugger:

anim.name = @"springOpen";

Each animation comes with an associated tracer. The tracer allows you to record all animation-related events, in a fast and efficient manner, allowing you to query and analyze them after animation completion. The below example starts the tracer and configures it to log all events on animation completion:

POPAnimationTracer *tracer = anim.tracer;
tracer.shouldLogAndResetOnCompletion = YES;
[tracer start];

See POPAnimationTracer.h for more details.

 

Testing

Pop has extensive unit test coverage. To install test dependencies, navigate to the root pop directory and type:

pod install

Assuming CocoaPods is installed, this will include the necessary OCMock dependency to the unit test targets.

 

Resources

A collection of links to external resources that may prove valuable:

 

Contributing

See the CONTRIBUTING file for how to help out.

 

License

Pop is released under a BSD License. See LICENSE file for details.

时间: 2024-05-25 17:07:05

[翻译] POP Facebook的动画开源库的相关文章

Facebook AI实验室开源相似性搜索库Faiss:性能高于理论峰值55%,提速8.5倍

在用户日常搜索过程中,一个经常出现的问题即大多数返回的网站结果拥有完全相同或者几乎一样的信息.而应用了相似性搜索的相似引擎即可为用户返回最恰当.最合适的结果,同时隐藏或者丢弃那些重复的数据. 但是,目前相似性搜索领域需要克服的难题即它的规模和运行速度.雷锋网近日了解到,Facebook的人工智能研究团队就称已在该问题上取得了重要进展.Facebook在新发布的论文<Billion-scale similarity search with GPUs>中表示,可在GPU 上实现十亿规模级的相似性搜

赶在2018年前推荐30个最火爆开源库

欢迎Follow我的GitHub, 关注我的CSDN. 其余参考Android目录. 转载请注明出入谢谢! http://blog.csdn.net/xiaole0313/article/details/78926083 这些是自2017年3月以来我最喜欢的30个新的Android库.他们中的一些还没有做好生产准备,但是使用它们可能会有很多乐趣.我希望你喜欢这些. 这里没有特别的顺序: 1. Matisse 这是一个美丽的本地图像和视频选择器.主要功能: 选择包括JPEG,PNG,GIF和包括M

我的Android进阶之旅】GitHub 上排名前 100 的 Android 开源库进行简单的介绍

GitHub Android Libraries Top 100 简介 本文转载于:https://github.com/Freelander/Android_Data/blob/master/Android-Librarys-Top-100.md 本项目主要对目前 GitHub 上排名前 100 的 Android 开源库进行简单的介绍, 至于排名完全是根据 GitHub 搜索 Java 语言选择 (Best Match) 得到的结果, 然后过滤了跟 Android 不相关的项目, 所以排名并

站在巨人的肩膀上,C++开源库大全

程序员要站在巨人的肩膀上,C++拥有丰富的开源库,这里包括:标准库.Web应用框架.人工智能.数据库.图片处理.机器学习.日志.代码分析等.   标准库 C++ Standard Library:是一系列类和函数的集合,使用核心语言编写,也是C++ISO自身标准的一部分. Standard Template Library:标准模板库 C POSIX library : POSIX系统的C标准库规范 ISO C++ Standards Committee :C++标准委员会 框架 C++通用框架

Android非常有用的开源库介绍整理

Android开源库 自己一直很喜欢Android开发,就如博客副标题一样,我想做个好的App. 在摸索过程中,GitHub上搜集了很多很棒的Android第三方库,推荐给在苦苦寻找的开发者,而且我会不定期的更新这篇文章. 本文的其他贡献者: ____sky____ 感谢~ 我的GitHub Android下的优秀开发库数不胜数,在本文中,我列举的多是开发流程中最常用的一些.如果你还想了解更多的Android开源库,可以查看我的GitHub Star,过滤Java选项,每一个库都是我认真查看或者

10个有用的第三方iOS开源库

CocoaPods 地址:https://github.com/CocoaPods/CocoaPods 教程:http://www.raywenderlich.com/12139/introduction-to-cocoapods 描述:可以很方便的管理第三方库,清晰知道项目引用的库有哪些和它们的版本. CocoaAsyncSocket 地址:https://github.com/robbiehanson/CocoaAsyncSocket 简述: 处理TCP/UDP 链接的开源库. Appira

使用开源库 MagicalRecord 操作 CoreData

MagicalRecord  https://github.com/magicalpanda/MagicalRecord 注意:  MagicalRecord 在 ARC 下运作,Core Data 是 ORM 方案,据说带来的麻烦比好处多,且 Core Data 建立的表没有主键,但对于对数据库没有性能要求,进行简单的数据操作完全够用,能简化无数的代码量. MagicalRecord In software engineering, the active record pattern is a

27个提升效率的iOS开源库推荐

DZNEmptyDataSet(UI,空表格视图解算器) PDTSimpleCalendar(UI,drop-in日历组件) MagicalRecord(实施活跃记录模式的Core Data助手) Chameleon(UI,色彩框架) Alamofire(Swift 网络) TextFieldEffects (UI,自定义外观的文本区域) GPUImage(快速图片处理) iRate(获取用户评价) GameCenterManager(快速管理游戏中心) PKRevealController 2

Android 使用SwipeActionAdapter开源库实现简单列表的左右滑动操作

  我们做listview左右滑动操作时,一般中情况下,都是像QQ那样,左滑弹出操作菜单(删除.编辑),然后选择菜单操作: 这样的效果不可谓不好,算是非常经典. 另外,有少数的APP,尤其是任务管理类的APP,更加注重listview的操作交互,例如ToDoList及滴答清单,这两个APP对任务的操作是直接通过滑动列表进行操作的:效果图如下:   gtihub上有一个开源项目,已经很好的实现了对该效果:https://github.com/wdullaer/SwipeActionAdapter