Facebook POP 使用指南
Facebook POP 使用指南
Pop是一個(gè)動(dòng)畫(huà)引擎,用以擴(kuò)展iOS、OSX的動(dòng)畫(huà)類(lèi)型。相較于iOS、OSX中的基本動(dòng)畫(huà)效果,Pop擴(kuò)展后支持彈簧動(dòng)畫(huà)效果與衰減動(dòng)畫(huà)效果,你可以用Pop動(dòng)畫(huà)引擎來(lái)構(gòu)建出真實(shí)的物理交互效果。它的API與Core Animation的API非常類(lèi)似,使用起來(lái)非常容易。Pop動(dòng)畫(huà)引擎已經(jīng)經(jīng)過(guò)了良好的測(cè)試,我們?cè)?Paper 應(yīng)用中進(jìn)行了大量使用。
安裝
Pop支持 CocoaPods 安裝,將下面一行代碼添加到你的項(xiàng)目中的 Podfile 中:
pod 'pop', '~> 1.0'注意,bug會(huì)在主分支上面進(jìn)行修復(fù),然后在指定的分支上進(jìn)行發(fā)布。如果你喜歡嘗試最新的不大穩(wěn)定的版本,你可以通過(guò)以下入口來(lái)訪問(wèn)主分支:
pod 'pop', :git => 'https://github.com/facebook/pop.git'使用
Pop 支持Core Animation 中的顯式動(dòng)畫(huà)類(lèi)型,你可以通過(guò)導(dǎo)入頭文件來(lái)使用它:
#import <pop/POP.h>開(kāi)始動(dòng)畫(huà)、停止動(dòng)畫(huà)與更新動(dòng)畫(huà)
開(kāi)始執(zhí)行一個(gè)動(dòng)畫(huà),你可以將動(dòng)畫(huà)添加到一個(gè)對(duì)象中:
POPSpringAnimation *anim = [POPSpringAnimation animation]; ... [layer pop_addAnimation:anim forKey:@"myKey"];停止一個(gè)動(dòng)畫(huà),你可以根據(jù)一個(gè)鍵值來(lái)從對(duì)象中移除掉:
[layer pop_removeAnimationForKey:@"myKey"];你也可以根據(jù)鍵值來(lái)查詢(xún)已經(jīng)存在的動(dòng)畫(huà),你可以在執(zhí)行動(dòng)畫(huà)效果的同時(shí)來(lái)修改toValue屬性來(lái)實(shí)時(shí)更新動(dòng)畫(huà)效果:
anim = [layer pop_animationForKey:@"myKey"]; if (anim) {/* update to value to new destination */anim.toValue = @(42.0); } else {/* create and start a new animation */.... }注意,雖然上述示例中用到了一個(gè)layer,但是Pop動(dòng)畫(huà)引擎是基于 NSObject 所寫(xiě)的一個(gè)category,任何繼承自NSObject的對(duì)象都可以使用Pop動(dòng)畫(huà)引擎。
動(dòng)畫(huà)類(lèi)型
Pop支持4種動(dòng)畫(huà)類(lèi)型:彈簧效果、衰減效果、基本動(dòng)畫(huà)效果與自定義動(dòng)畫(huà)效果。
彈簧效果可以用來(lái)實(shí)現(xiàn)仿真的物理彈簧特效,在下面的這個(gè)例子中,我們用彈簧效果來(lái)對(duì)一個(gè)layer的尺寸進(jìn)行縮放:
效果圖:
源碼:
#import "ViewController.h" #import "POP.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];// 創(chuàng)建layerCALayer *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];// 執(zhí)行Spring動(dòng)畫(huà)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衰減效果可以用來(lái)模擬真實(shí)的物理減速效果,在下面的例子中,我們用衰減效果來(lái)對(duì)一個(gè)view的拖拽停止執(zhí)行減速效果。
效果圖:
源碼:
#import "ViewController.h" #import "POP.h"@interface ViewController ()<POPAnimationDelegate>@property(nonatomic) UIControl *dragView;@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];// 初始化dragViewself.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:selfaction:@selector(touchDown:)forControlEvents:UIControlEventTouchDown];// 添加手勢(shì)UIPanGestureRecognizer *recognizer = [[UIPanGestureRecognizer alloc] initWithTarget:selfaction:@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];// 拖拽動(dòng)作結(jié)束if(recognizer.state == UIGestureRecognizerStateEnded){// 計(jì)算出移動(dòng)的速度CGPoint velocity = [recognizer velocityInView:self.view];// 衰退減速動(dòng)畫(huà)POPDecayAnimation *positionAnimation = \[POPDecayAnimation animationWithPropertyNamed:kPOPLayerPosition];// 設(shè)置代理positionAnimation.delegate = self;// 設(shè)置速度動(dòng)畫(huà)positionAnimation.velocity = [NSValue valueWithCGPoint:velocity];// 添加動(dòng)畫(huà)[recognizer.view.layer pop_addAnimation:positionAnimationforKey:@"layerPositionAnimation"];} }@end基本動(dòng)畫(huà)效果可以指定具體的動(dòng)畫(huà)時(shí)間,與 CoreAnimation 中的 CABasicAnimation 的用法極為類(lèi)似,在下面的例子中,我們用基本動(dòng)畫(huà)效果來(lái)設(shè)置一個(gè)view的alpha值。
效果圖:
源碼:
#import "ViewController.h" #import "POP.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];// 創(chuàng)建viewUIView *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];// 執(zhí)行基本動(dòng)畫(huà)效果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自定義動(dòng)畫(huà)效果是根據(jù) CADisplayLink 來(lái)計(jì)算出中間的差值,然后由你來(lái)處理輸出的值的動(dòng)畫(huà)效果,詳情請(qǐng)參考相關(guān)頭文件來(lái)獲取更多的細(xì)節(jié)。
屬性
屬性是通過(guò) POPAnimatableProperty 來(lái)定義的。在下面的這個(gè)例子中,我們創(chuàng)建出了一個(gè)彈簧動(dòng)畫(huà)效果并顯式的設(shè)置它去響應(yīng) -[CALayer bounds]:
POPSpringAnimation *anim = [POPSpringAnimation animation]; anim.property = [POPAnimatableProperty propertyWithName:kPOPLayerBounds];Pop動(dòng)畫(huà)引擎本身提供了很多可以做動(dòng)畫(huà)的屬性供你定制。你可以在這個(gè)類(lèi)里面創(chuàng)建出一個(gè)實(shí)例對(duì)象:
prop = [POPAnimatableProperty propertyWithName:@"com.foo.radio.volume" initializer:^(POPMutableAnimatableProperty *prop) {// read valueprop.readBlock = ^(id obj, CGFloat values[]) {values[0] = [obj volume];};// write valueprop.writeBlock = ^(id obj, const CGFloat values[]) {[obj setVolume:values[0]];};// dynamics thresholdprop.threshold = 0.01; }];anim.property = prop;為了了解更多的可以做動(dòng)畫(huà)效果的屬性,你可以參考 POPAnimatableProperty.h 查看更多的細(xì)節(jié)。
相關(guān)資源
以下是一些示例供你學(xué)習(xí):
- 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
轉(zhuǎn)載于:https://www.cnblogs.com/YouXianMing/p/4398232.html
總結(jié)
以上是生活随笔為你收集整理的Facebook POP 使用指南的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 构建ASP.NET MVC4+EF5+E
- 下一篇: MySQL配置主从同步过程记录