Skip to content

Instantly share code, notes, and snippets.

@yingmu52
Last active June 5, 2018 03:36
Show Gist options
  • Save yingmu52/ba0c98f43727a0cd1fa9a823ed7d7ab8 to your computer and use it in GitHub Desktop.
Save yingmu52/ba0c98f43727a0cd1fa9a823ed7d7ab8 to your computer and use it in GitHub Desktop.
ViewController Transition Animator
import Foundation
import UIKit
public struct Animation {
public let duration: TimeInterval
public let closure: (UIView) -> Void
public init(duration: TimeInterval, closure: @escaping (UIView) -> Void) {
self.duration = duration
self.closure = closure
}
}
public extension UIView {
func animate(inSerial animations: [Animation]) {
if animations.isEmpty { return }
var animations = animations
let animation = animations.removeFirst()
UIView.animate(
withDuration: animation.duration,
animations: { animation.closure(self) },
completion: { _ in self.animate(inSerial: animations)}
)
}
func animate(inParallel animations: [Animation]) {
for animation in animations {
UIView.animate(withDuration: animation.duration) { animation.closure(self) }
}
}
}
public extension Animation {
static func fadeIn(duration: TimeInterval = 0.3) -> Animation {
return Animation(duration: duration, closure: { $0.alpha = 1 })
}
static func fadeOut(duration: TimeInterval = 0.3) -> Animation {
return Animation(duration: duration) { $0.alpha = 0 }
}
static func resize(to size: CGSize, duration: TimeInterval = 0.3) -> Animation {
return Animation(duration: duration, closure: { $0.bounds.size = size })
}
static func move(byX x: CGFloat, y: CGFloat, duration: TimeInterval = 0.3) -> Animation {
return Animation(duration: duration) {
$0.center.x += x
$0.center.y += y
}
}
}
//: Playground - noun: a place where people can play
import UIKit
var str = "Hello, playground"
import Foundation
import UIKit
import PlaygroundSupport
extension URL {
subscript(queryParam: String) -> String? {
guard let url = URLComponents(string: absoluteString) else { return nil }
return url
.queryItems?
.first { $0.name == queryParam }?
.value
}
}
//let url = URL(string: "rexeipt-stg://ca.myfind/recover?recoveryCode=something")!
let url = URL(string: "rexeipt://ca.CDCDCNDSXS")!
//print(url["recoveryCode"])
enum DeepLink {
case recover(String)
case offer(String)
case qrcode(String)
case unsupported
init?(_ url: URL) {
switch (url.scheme, url.host, url.path) {
case ("rexeipt-stg", "ca.myfind", "/recover"):
if let code = url["recoveryCode"] {
self = .recover(code)
} else {
self = .unsupported
}
case ("rexeipt-stg", "ca.myfind", "/offer"):
if let objectId = url["objectId"] {
self = .offer(objectId)
} else {
self = .unsupported
}
case ("rexeipt", let code?, let path) where path.isEmpty:
self = .qrcode(code)
default:
self = .unsupported
}
}
}
let d = DeepLink(url)
print(d)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment