Code Monkey home page Code Monkey logo

dark-mode-toggle's Introduction

Dark Mode Toggle ๐ŸŒŒ ๐ŸŒ

After seeing the animation when toggling Dark Theme in Slack for iOS, I thought I'd try my hand at replicating the animation. After that I wondered how I could spice up the animation by doing something different.

How is this done?

This effect is achieved by leveraging a few techniques:

  • Create a snapshot of the current window's view using snapshotView(afterScreenUpdates:)
  • Add the snapshotView to the window's rootViewController.view
  • Create a layer mask shape using CAShapeLayer
  • Add the layer mask to the snapshotView
  • Use CABasicAnimation to animate the mask's path
  • Finally, remove the snapshotView when the animation finishes

CABasicAnimation notes

By default CABasicAnimation returns to the initial state once the animation finishes. You can prevent this behaviour by setting:

animation.fillMode = .forwards
animation.isRemovedOnCompletion = false

Now the animation will stay at it's end state.

Additionally we use a thin closure wrapper around CAAnimationDelegate's animationDidStop(_:finished:) method, to create a more modern completion block:

animation.delegate = AnimationDelegate { finished in
    snapshotView.removeFromSuperview()
    animation.delegate = nil
    completion?(finished)
}

Demo

Linear Circular
Linear Reveal Animation Circular Reveal Animation

dark-mode-toggle's People

Contributors

squarefrog avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.