iOSiOS自我学习库iOS 开发摘记(Swift)

lottie-ios 的集成及使用

2018-04-18  本文已影响647人  奇怪的她的他

Lottie 是一个可应用于Andriod和iOS的动画库,它通过bodymovin插件来解析Adobe After Effects 动画并导出为json文件,通过手机端原生的方式或者通过React Native的方式渲染出矢量动画。
lottie三方框架地址:https://github.com/airbnb/lottie-ios

总而言之,咱们开发人员有了这个三方框架,再也不用去苦恼各种动画的实现了,还得跟UI设计人员扯皮。这个框架,UI设计人员将动画图制作好了后,利用工具转为json文件,咱们通过框架提供的方法加载json就可以实现各种精彩的动画,但是有一个缺点,这个框架的动画效果只能作为展示,不能产生交互行为。进入正题,开始集成。

一、通过cocoapods集成。

1.在你的podfile中添加:
pod 'lottie-ios'

2.运行
pod install

二、安装Carthage

1、安装 Homebrew
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

第二步:

brew update
2、安装 Carthage
brew install carthage

可通过下面这条命令来查看版本。

carthage version
3、使用 Carthage 安装依赖

第一步:

cd ~/路径/项目文件夹

第二步:创建一个空的 Carthage 文件 Cartfile

touch Cartfile

第三步:使用 Xcode 打开 Cartfile 文件

open -a Xcode Cartfile

第四步:在cartfile里面加一行代码

github "airbnb/lottie-ios" "master"

第五步:终端执行更新命令

carthage update --platform iOS
OK,你再编译项目试试,,这个时候code1错误没有了。是不是很惊喜。注意:项目名字最好为英文,因为这个框架是国外的,假如项目名字包含中文也会出现想像不到的错误。

那么问题来了,这个时候可能会出现这种情况:导入头文件还有索引,但是导入后总是报错,报找不到那个头文件,这种时候,只需多重启几次Xcode。就能OK

三、lottie的使用

Lottie支持iOS 8 及其以上系统。当我们把动画需要的images资源文件添加到Xcode的目标工程的后,Lottie 就可以通过JSON文件或者包含了JSON文件的URL来加载动画。

LOTAnimationView *animation = [LOTAnimationView animationNamed:@"Lottie"];
[self.view addSubview:animation];
[animation playWithCompletion:^(BOOL animationFinished) {
  // Do Something
}];
LOTAnimationView *animation = [LOTAnimationView animationNamed:@"Lottie" inBundle:[NSBundle YOUR_BUNDLE]];
[self.view addSubview:animation];
[animation playWithCompletion:^(BOOL animationFinished) {
  // Do Something
}];
LOTAnimationView *animation = [[LOTAnimationView alloc] initWithContentsOfURL:[NSURL URLWithString:URL]];
[self.view addSubview:animation];

Lottie 支持iOS中的UIViewContentModes的 aspectFit, aspectFill 和 scaleFill这些属性。

CGPoint translation = [gesture getTranslationInView:self.view];
CGFloat progress = translation.y / self.view.bounds.size.height;
animationView.animationProgress = progress;
UIView *snapshot = [self.view snapshotViewAfterScreenUpdates:YES];
[lottieAnimation addSubview:snapshot toLayerNamed:@"AfterEffectsLayerName"];
-(id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented
                                                                  presentingController:(UIViewController *)presenting
                                                                      sourceController:(UIViewController *)source {
  LOTAnimationTransitionController *animationController = [[LOTAnimationTransitionController alloc] initWithAnimationNamed:@"vcTransition1"
                                                                                                          fromLayerNamed:@"outLayer"
                                                                                                            toLayerNamed:@"inLayer"];
  return animationController;
}
上一篇下一篇

猜你喜欢

热点阅读