在Xcode中使用Lottie-OC
2019-12-18 本文已影响0人
Jamesgjh
用CocoaPods安装Lottie动画库
启动 Xcode 创建项目,项目路径必须是英文,比如:LottieDemo并保存。创建完项目必须完全退出 Xcode。打开Terminal,cd到自己的项目目录下(cd +空格+直接拖拽项目到终端),在terminal中运行 pod init 指令来创建一个Podfile。
打开Podfile文件
# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'
target 'AnswerWindow' do
# Uncomment the next line if you're using Swift or would like to use dynamic frameworks
# use_frameworks!
# Pods for AnswerWindow
pod 'lottie-ios'
end
我们只加一行pod 'lottie-ios'来指出这是这个项目需要的pod。然后回Terminal运行下列指令:
pod install
CocoaPods会下载Lottie动画库并在Xcode项目中打包。打包结束后你会发现出现一个叫LottieDemo.xcworkspace.的新项目文件。在Xcode中打开这个文件并开始进行编码。
lottie的使用
#import "ViewController.h"
#import <Lottie/Lottie.h>
@interface ViewController ()
@property (nonatomic,strong) LOTAnimationView *lottieHeart;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.lottieHeart = [LOTAnimationView animationNamed:@"heart"];
self.lottieHeart.contentMode = UIViewContentModeScaleAspectFit;
// self.heart.backgroundColor = [UIColor redColor];
[self.view addSubview:self.lottieHeart];
self.lottieHeart.loopAnimation = YES;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.lottieHeart play];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.lottieHeart pause];
}
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
CGRect lottieRect = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
self.lottieHeart.frame = lottieRect;
}
@end