Created
October 13, 2014 18:39
-
-
Save elliottkember/549a5c861fc790c67d58 to your computer and use it in GitHub Desktop.
A layer that fades between images depending on its state
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ImageSwappingLayer extends Layer | |
images: {} | |
imageSubLayer: false | |
imageTransitionTime: 0.5 | |
constructor: (options={}) -> | |
super options | |
@imageTransitionTime = options.imageTransitionTime if options.imageTransitionTime | |
@fadeToBackgroundImage options.image if options.image | |
@on Events.StateWillSwitch, => | |
@fadeToBackgroundImage image if image = @images[@states.current] | |
for event in ["change:width", "change:height"] | |
@on event, @resizeSubLayer.bind @ | |
fadeToBackgroundImage: (image) -> | |
oldImageSubLayer = @imageSubLayer | |
newImageSubLayer = new Layer image: image, width: @width, height: @height, backgroundColor: 'red', superLayer: @, opacity: 0 | |
setTimeout => | |
newImageSubLayer.animate time: @imageTransitionTime, properties: opacity: 1 | |
.on "end", -> oldImageSubLayer.destroy() if oldImageSubLayer | |
@imageSubLayer = newImageSubLayer | |
resizeSubLayer: () -> | |
@imageSubLayer.frame = @frame |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: