ios中應用Lottie解決動畫問題

作為一名iOS工程師,深知復雜動畫的痛苦,下面我們來介紹下Lottie來解決日常的動畫難題。

Lottie的簡單介紹:

  • 使用Lottie開發的流程是: 設計師在AE中設計完成你的動畫,通過bodymoving插件導出紀錄動畫信息的JSON文件,然后開發人員使用 Lottie 的Android,iOS,React Native apps開源動畫庫讀取這份JSON文件, 解析動畫結構和參數信息并渲染。

Lottie的優點:

  1. 設計即所見: 設計師用AE設計好動畫后直接導出Json文件,Lottie 解析Json文件后調Core Animation的API繪制渲染。還原度更好,開發成本更低。
  2. 跨平臺: 支持iOS、Android、React Native。
  3. 性能:Lottie對于從AE導出的Json文件,用Core Animation做矢量動畫, 性能較佳。Lottie 對解析后的數據模型有內存緩存。但是對多圖片幀動畫,性能比較差。
    支持動畫屬性多:比起臉書的Keyframes,Lottie支持了更多AE動畫屬性,比如Mask, Trim Paths,Stroke (shape layer)等。
  4. 包大小,相比動輒上百K的幀動畫,Json文件包大小很小。有圖片資源的情況下,同一張圖片也可以被多個圖層復用,而且運行時內存中只有一個UIImage對象(iOS)。

Lottie在iOS中的使用

  1. pod 'lottie-ios' 使用cocoaPods來加載Lottie。
  2. 在使用的界面添加頭文件#import <Lottie/Lottie.h>
  3. 簡單的使用介紹(要想深入學習,還需要自己點擊進入源代碼中去深究每一個方法和屬性,在此就不一一列舉了)
LOTAnimationView * animation = [LOTAnimationView animationNamed:@"HappyBirthday"];
    animation.loopAnimation = YES; //是否是循環播放
    animation.frame  = self.view.bounds;
    [self.view addSubview:animation];
    animation.backgroundColor = [UIColor whiteColor];
    [animation playWithCompletion:^(BOOL animationFinished) {
        //播放完成,循環播放則不進入此方法
    }];
    //可以以動畫為北京來添加子控件
    UILabel * newV = [[UILabel alloc]initWithFrame:CGRectMake(100,100,200,100)];
    newV.backgroundColor = [UIColor clearColor];
    newV.textColor = [UIColor blackColor];
    newV.text = @"Lottie的使用教程";
    [animation addSubview:newV];

另外的創建方法

/// Load animation by name from the default bundle, Images are also loaded from the bundle
+ (nonnull instancetype)animationNamed:(nonnull NSString *)animationName NS_SWIFT_NAME(init(name:));

/// Loads animation by name from specified bundle, Images are also loaded from the bundle
+ (nonnull instancetype)animationNamed:(nonnull NSString *)animationName inBundle:(nonnull NSBundle *)bundle NS_SWIFT_NAME(init(name:bundle:));

/// Creates an animation from the deserialized JSON Dictionary
+ (nonnull instancetype)animationFromJSON:(nonnull NSDictionary *)animationJSON NS_SWIFT_NAME(init(json:));

/// Loads an animation from a specific file path. WARNING Do not use a web URL for file path.
+ (nonnull instancetype)animationWithFilePath:(nonnull NSString *)filePath NS_SWIFT_NAME(init(filePath:));

/// Creates an animation from the deserialized JSON Dictionary, images are loaded from the specified bundle
+ (nonnull instancetype)animationFromJSON:(nullable NSDictionary *)animationJSON inBundle:(nullable NSBundle *)bundle NS_SWIFT_NAME(init(json:bundle:));

/// Creates an animation from the LOTComposition, images are loaded from the specified bundle
- (nonnull instancetype)initWithModel:(nullable LOTComposition *)model inBundle:(nullable NSBundle *)bundle;

/// Loads animation asynchrounously from the specified URL
- (nonnull instancetype)initWithContentsOfURL:(nonnull NSURL *)url;

LOTAnimationView的屬性

/// Flag is YES when the animation is playing
@property (nonatomic, readonly) BOOL isAnimationPlaying;

/// Tells the animation to loop indefinitely.
@property (nonatomic, assign) BOOL loopAnimation;

/// The animation will play forward and then backwards if loopAnimation is also YES
@property (nonatomic, assign) BOOL autoReverseAnimation;

/// Sets a progress from 0 - 1 of the animation. If the animation is playing it will stop and the compeltion block will be called.
/// The current progress of the animation in absolute time.
/// e.g. a value of 0.75 always represents the same point in the animation, regardless of positive
/// or negative speed.
@property (nonatomic, assign) CGFloat animationProgress;

/// Sets the speed of the animation. Accepts a negative value for reversing animation.
@property (nonatomic, assign) CGFloat animationSpeed;

/// Read only of the duration in seconds of the animation at speed of 1
@property (nonatomic, readonly) CGFloat animationDuration;

/// Enables or disables caching of the backing animation model. Defaults to YES
@property (nonatomic, assign) BOOL cacheEnable;

/// Sets a completion block to call when the animation has completed
@property (nonatomic, copy, nullable) LOTAnimationCompletionBlock completionBlock;

/// Set the amimation data
@property (nonatomic, strong, nullable) LOTComposition *sceneModel;
  1. 簡單應用的場景:(1)App的動畫引導頁。(2)一些特定的動畫界面。(3)來作為Tabbar來使用。
  2. 這里來介紹下作為Tabbar的使用gitHub上原作者
  3. Lottie動畫資源網站
  4. 后續有新的學習會更新的。
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容