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

Unable to implement intro/looping gif pair #105

Open
Chrisdf opened this issue Apr 7, 2017 · 1 comment
Open

Unable to implement intro/looping gif pair #105

Chrisdf opened this issue Apr 7, 2017 · 1 comment

Comments

@Chrisdf
Copy link

Chrisdf commented Apr 7, 2017

I am trying to implement a system where a GIFImageView will load a lead-in GIF, and then after the first GIF has played fully it will reset the GIFImageView to loop a secondary GIF indefinitely.

Currently, I have to implement the functions as follows:

extension GIFImageView {
    
    public func queueAnimation(secondGIFName: String) {
        self.startAnimatingGIF()
        
        DispatchQueue.main.asyncAfter(deadline: .now() + self.gifLoopDuration) {
                
            self.prepareForAnimation(withGIFNamed: secondGIFName)
            self.startAnimatingGIF()
        }
    }
}

And in my ViewController:

 override public  func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.welcomeImage.prepareForAnimation(withGIFNamed: "Welcome_IN", loopCount: 1)
    }
    
    override public func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        self.welcomeImage.queueAnimation(secondGIFName: "Welcome_LOOP")
    }

I have to do it this way because in the following code, the variable gifLoopDuration will always be 0 if the 'prepareForAnimation()' method is called inside the extension:

extension GIFImageView {
    
    public func queueAnimation(secondGIFName: String) {
        self.welcomeImage.prepareForAnimation(withGIFNamed: "Welcome_IN", loopCount: 1)
        self.startAnimatingGIF()
        
        DispatchQueue.main.asyncAfter(deadline: .now() + self.gifLoopDuration) {
                
            self.prepareForAnimation(withGIFNamed: secondGIFName)
            self.startAnimatingGIF()
        }
    }
}

The reason why it is always set to 0 is because all of the frame loading is offloaded to an internal queue in FrameLoader.swift, and there is no way to access that queue from outside the Animator class.

What I would like to be changed is have a method (possibly inside GIFImageView) that will return the animation time for a loaded GIF in a closure, so that all the frames can still be loaded asynchronously but we still can have the knowledge of how long the loaded GIF will take.

@robinst
Copy link
Contributor

robinst commented Aug 23, 2022

@Chrisdf Not sure if this existed when you opened this issue, but there's a completionHandler argument to prepareForAnimation:

public func prepareForAnimation(withGIFNamed imageName: String,
loopCount: Int = 0,
completionHandler: (() -> Void)? = nil) {

That's called when the GIF has been prepared and you can safely read gifLoopDuration there.

I think this exact issue can be closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants