準備工作:
需要一個動畫的json的文件,或者json字符串,一般由UI設計人員生成的,直接導入到project裏面就可以;
1 在Podfile裏面引入第三方庫lottie-ios,
版本2.5.3是objective-c的版本
也可以引入最新的版本是swift的版本:pod 'lottie-ios', :git => 'https://github.com/airbnb/lottie-ios.git'
Podfile內容如下:
# platform :ios, '9.0'
target 'LottieTest' do
use_frameworks!
pod 'lottie-ios', '=2.5.3'
end
2 在mac的終端中 pod install
3 直接在代碼中應用動畫的方法
代碼如下:
#import "ViewController.h"
#import "Lottie.h"
@interface ViewController ()
{
LOTAnimationView *_loadingView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
if (!_loadingView) {
_loadingView = [LOTAnimationView animationNamed:@"xxxxxjsonfile"];
_loadingView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.width);
_loadingView.center = self.view.center;
_loadingView.loopAnimation = YES;
_loadingView.contentMode = UIViewContentModeScaleAspectFill;
_loadingView.animationSpeed = 0.5;
}
[self.view addSubview:_loadingView];
[_loadingView play];
}