Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for setting animation after init #347

Merged
merged 1 commit into from
Aug 4, 2017
Merged

Conversation

buba447
Copy link
Collaborator

@buba447 buba447 commented Aug 4, 2017

@willyantows
Copy link

@buba447 Can you give me an example code how to setting animation after init? (Swift version is better)
Thanks

@PizhuLoveCode
Copy link

@buba447 Now I create only one LOTAnimationView,then by config the LOTCompostion,can I animate A switch to animate B then return to animate A?

@willyantows
Copy link

@PizhuLoveCode Can you give sample code?

@PizhuLoveCode
Copy link

PizhuLoveCode commented Aug 16, 2017

@Willyanto my code is a little stupid, if u find better way,please tell me.3q

//init
NSString *jsonPath = [[NSBundle mainBundle]pathForResource:@"chinese" ofType:@"json"];
NSData *jsonData = [[NSData alloc]initWithContentsOfFile:jsonPath];
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:nil];

NSString *jsonPath2 = [[NSBundle mainBundle]pathForResource:@"us" ofType:@"json"];
NSData *jsonData2 = [[NSData alloc]initWithContentsOfFile:jsonPath2];
NSDictionary *dict2 = [NSJSONSerialization JSONObjectWithData:jsonData2 options:NSJSONReadingAllowFragments error:nil];


self.sceneModel1 = [[LOTComposition alloc] initWithJSON:dict withAssetBundle:[NSBundle mainBundle]];
self.sceneModel2 = [[LOTComposition alloc] initWithJSON:dict2 withAssetBundle:[NSBundle mainBundle]];

//play1:
self.lottieLogo = [[LOTAnimationView alloc] initWithModel:self.sceneModel1 inBundle:[NSBundle mainBundle]];
self.lottieLogo.contentMode = UIViewContentModeScaleAspectFill;
self.lottieLogo.loopAnimation = YES;
[self.lottieLogo play];

//swtich to play2

self.lottieLogo.sceneModel = _sceneModel2;
[self.lottieLogo play];
self.lottieLogo.loopAnimation = YES;

@willyantows
Copy link

@PizhuLoveCode wow thank you bro, it's work. I think it's pretty enough for now.

@1pocketaces1
Copy link

Hi is there any example of doing this with ng-lottie in angular?

@bitcloudTheFirst
Copy link

This seems like a great feature. Can anyone offer up an example in javascript? Thanks

@prufect
Copy link

prufect commented Feb 28, 2020

Here is a small swift extension (with error handling) that you can drop in your code

extension LOTAnimationView {
    
    enum SetAnimationError: LocalizedError {
        case PathError(String)
        case JsonSerializationError(String)
        
        var errorDescription: String? {
        switch self {
            case .PathError(let message):
                return message
            case .JsonSerializationError(let message):
                return message
            }
        }
    }
    
    func setAnimation(to jsonFile: String, autoPlay: Bool = true, loop: Bool = false) throws {
        if let path = Bundle.main.path(forResource: jsonFile, ofType: "json") {
            do {
                let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .mappedIfSafe)
                let jsonResult = try JSONSerialization.jsonObject(with: data, options: .mutableLeaves)
                if let jsonResult = jsonResult as? Dictionary<String, AnyObject>{
                    if let sceneModel = LOTComposition(json: jsonResult, withAssetBundle: nil) {
                        self.sceneModel = sceneModel
                        if loop { self.loopAnimation = true }
                        if autoPlay { self.play() }
                    }
                }
            } catch {
                throw SetAnimationError.JsonSerializationError("Failed to convert your asset to a json dictionary")
            }
        } else {
            throw SetAnimationError.PathError("Could not find \(jsonFile) in your main bundle")
        }
    }
}

Example Usage:

do {
      try animView.setAnimation(to: "animName")
  } catch let error {
      print(error.localizedDescription)
 }

NSGolova pushed a commit to clario-tech/lottie-ios that referenced this pull request Nov 19, 2020
Add support for setting animation after init
calda pushed a commit that referenced this pull request Dec 1, 2022
Add support for setting animation after init
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants