Created
November 30, 2017 06:16
-
-
Save hmhmsh/64b66f53d0053e7459b54f7ac8b32a50 to your computer and use it in GitHub Desktop.
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
// | |
// FadeInOut.swift | |
// | |
// | |
// Usage | |
// フェードイン | |
// 完了タイミングで何かしたい場合はクロージャーも記述 | |
// FadeView.fadeIn(type: .Slow) { [weak self] in | |
// println("FadeIn 完了") | |
// } | |
// | |
import Foundation | |
import UIKit | |
enum FadeType: NSTimeInterval { | |
case | |
Normal = 0.2, | |
Slow = 1.0 | |
} | |
extension UIView { | |
func fadeIn(type: FadeType = .Normal, completed: (() -> ())? = nil) { | |
fadeIn(type.rawValue, completed: completed) | |
} | |
/** For typical purpose, use "public func fadeIn(type: FadeType = .Normal, completed: (() -> ())? = nil)" instead of this */ | |
func fadeIn(duration: NSTimeInterval = FadeType.Slow.rawValue, completed: (() -> ())? = nil) { | |
alpha = 0 | |
hidden = false | |
UIView.animateWithDuration(duration, | |
animations: { | |
self.alpha = 1 | |
}) { finished in | |
completed?() | |
} | |
} | |
func fadeOut(type: FadeType = .Normal, completed: (() -> ())? = nil) { | |
fadeOut(type.rawValue, completed: completed) | |
} | |
/** For typical purpose, use "public func fadeOut(type: FadeType = .Normal, completed: (() -> ())? = nil)" instead of this */ | |
func fadeOut(duration: NSTimeInterval = FadeType.Slow.rawValue, completed: (() -> ())? = nil) { | |
UIView.animateWithDuration(duration | |
, animations: { | |
self.alpha = 0 | |
}) { [weak self] finished in | |
self?.hidden = true | |
self?.alpha = 1 | |
completed?() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment