可视样式间的转换效果 - Gagat

2017-06-13      705      Swift
项目简介

Gagat

Gagat is a small Swift library that makes it easy to add a delightful, interactive way to switch between two different themes in your iOS application using a two-finger pan. The library was designed primarily for applications that support a "dark mode", and is heavily inspired by the Night Mode transition in Castro 2

What Gagat is not

Gagat is not a library for styling your iOS applications. You must write all the styling logic yourself without any help from Gagat, and then call your styling code from your implementation ofGagatStyleable.toggleActiveStyle(). If you are unsure of how to implement the styling, the Example app included with Gagat showcases one way to support different themes in an application by using configuration models .

Requirements

Gagat is written in Swift 3.1 and requires iOS 10.0. It cannot be used from applications written in Objective-C.

Installation

The recommended way to install Gagat is using Carthage . Simply add the following line to yourCartfile:

github "Boerworz/Gagat" ~> 1.0

If you prefer not to use a dependency managment system you can copy the swift files from the Gagat directory into your project.

Usage

To support Gagat in your iOS application there's two things you need to do (apart fromimporting Gagat):

1. Conform toGagatStyleable

In order for Gagat to be able to trigger the style change in your application you must provide it with an object that conforms toGagatStyleable. This object could be your root view controller, a specializedStyleManagerkind of object, your app delegate, or any other object that fits your architecture.

The only requirement in theGagatStyleableprotocol is that you implement atoggleActiveStyle()method. When this method is invoked you should switch to the theme that is currently inactive, e.g. switch from a light theme to a dark theme or vice versa. It's not strictly a requirement, but for the best results you should attempt to have completed the style change before you return fromtoggleActiveStyle()(i.e. avoid asynchronous calls if possible).

2. CallGagat.configure(for:with:using:)

This is typically done in your implementation ofapplication(_:didFinishLaunchingWithOptions:)and tells Gagat to get ready to handle the interactive transition in the specified window (probably your application's only window). The second argument is your object that conforms toGagatStyleable.

If you want to customize properties of the transition, such as the jelly factor, pass an instance ofGagat.Configurationas the last argument toGagat.configure(for:with:using:), or omit it if you want to use the defaults.

Important:In order for Gagat to work as expected you must keep a reference to the value returned byGagat.configure(for:with:using:)for the entire lifetime of your application (or until you want to disable Gagat).

// AppDelegate.swift

var window: UIWindow?
var gagatTransitionHandle: Gagat.TransitionHandle!

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
	let configuration = Gagat.Configuration(jellyFactor: 1.5)
	let styleableViewController = window!.rootViewController as! GagatStyleable
	gagatTransitionHandle = Gagat.configure(for: window!, with: styleableViewController, using: configuration)
	
	return true
}

You're done!

You should now be able to switch between your different styles by using a two-finger downwards pan from anywhere in your application.

Running the Example app

Gagat includes an app that showcases what this library has to offer. To run the Example app, open Gagat.xcodeproj and run the Gagat Example scheme. If you want to run the Example app on a device (you do!) then you'll need to select a team in the target settings for the Gagat Example target.