Gifu icon indicating copy to clipboard operation
Gifu copied to clipboard

Unable to implement intro/looping gif pair

Open Chrisdf opened this issue 7 years ago • 1 comments

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.

Chrisdf avatar Apr 07 '17 20:04 Chrisdf

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

https://github.com/kaishin/Gifu/blob/e6e71c0220b7a4b21791b5e7149a3bb47afa1d2e/Sources/Gifu/Classes/GIFAnimatable.swift#L110-L112

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

I think this exact issue can be closed.

robinst avatar Aug 23 '22 16:08 robinst