Last active
September 20, 2016 04:52
-
-
Save dedeexe/6e0d5dd311a2ea95c03dbd014663e83c to your computer and use it in GitHub Desktop.
Creating a kind of toast
This file contains 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
import UIKit | |
extension UIView { | |
private struct FloatingKeys { | |
static var FKCompletion : String = "FKCompletion" | |
} | |
private class CompletionWrapper { | |
private var completion : (()->Void)? = nil | |
init(_ completion : (()->Void)? = nil) { | |
self.completion = completion | |
} | |
func executeCompletion() { | |
completion?() | |
} | |
} | |
private func createBaseAlertView(view:UIView) -> UIView | |
{ | |
let offset = CGFloat(15) | |
let padding = CGFloat(15) | |
let buttonSize = CGFloat(30) | |
let coloredView = UIView() | |
let closeButton = UIButton() | |
let containerView = UIView() | |
let insideView = view | |
let componentView = UIView() | |
containerView.addSubview(coloredView) | |
containerView.addSubview(insideView) | |
componentView.addSubview(containerView) | |
componentView.addSubview(closeButton) | |
let insideViewSize = insideView.bounds | |
componentView.frame = CGRect(origin: CGPointZero, | |
size: CGSize( width: self.frame.width, | |
height: insideViewSize.height + offset * 3)) | |
containerView.frame = CGRect(x: 0, | |
y: offset, | |
width: componentView.frame.width, | |
height: insideViewSize.height + offset * 2) | |
coloredView.frame = CGRect(x: 0, | |
y: 0, | |
width: componentView.frame.width, | |
height: 3) | |
insideView.frame = CGRect(x: padding, | |
y: offset, | |
width: containerView.frame.width - padding - padding, | |
height: insideViewSize.height) | |
closeButton.frame = CGRect(x: componentView.frame.width - buttonSize - padding, | |
y: 4, | |
width: buttonSize, | |
height: buttonSize) | |
let value = CGFloat(250.0/255.0) | |
containerView.backgroundColor = UIColor(red: value, green: value, blue: value, alpha: 1.0) | |
coloredView.backgroundColor = UIColor.redColor() | |
componentView.backgroundColor = UIColor.clearColor() | |
closeButton.setImage(UIImage(named:"icCloseButton"), forState: .Normal) | |
closeButton.addTarget(self, action: #selector(handleCloseButton), forControlEvents: .TouchUpInside) | |
return componentView | |
} | |
private func createFloatingAlertView(view:UIView) -> UIView { | |
let externalView = createBaseAlertView(view) | |
return externalView | |
} | |
public func showFloatingView(message message:String, completion:(()->Void)? = nil) | |
{ | |
let padding = CGFloat(15) | |
let messageLabel = UILabel() | |
messageLabel.text = message | |
messageLabel.font = UIFont.systemFontOfSize(12) | |
messageLabel.textColor = UIColor(colorLiteralRed: 82/255.0, green: 84/255.0, blue: 91/255.0, alpha: 1.0) | |
messageLabel.numberOfLines = 0 | |
let sizeLabel = messageLabel.sizeThatFits(CGSize(width:self.frame.width - padding * 2, height: 10000)) | |
messageLabel.frame.size = sizeLabel | |
showFloatingView(view: messageLabel, completion: completion) | |
} | |
public func showFloatingView(view view:UIView, completion:(()->Void)? = nil) | |
{ | |
let floatingView = createFloatingAlertView(view) | |
objc_setAssociatedObject(floatingView, &FloatingKeys.FKCompletion, CompletionWrapper(completion), .OBJC_ASSOCIATION_RETAIN_NONATOMIC) | |
floatingView.frame.origin.y = self.frame.height + 10 | |
self.addSubview(floatingView) | |
let finalPosition = self.frame.height - floatingView.frame.height | |
UIView.animateWithDuration(0.3, delay: 0.0, options: UIViewAnimationOptions.CurveEaseIn, animations: { | |
floatingView.frame.origin.y = finalPosition | |
}, completion: nil) | |
} | |
private func hideFloatingView(view:UIView) | |
{ | |
let finalPosition = self.frame.height + 10 | |
UIView.animateWithDuration(0.3, delay: 0.0, options: UIViewAnimationOptions.CurveEaseIn, animations: { | |
view.frame.origin.y = finalPosition | |
}, completion: { (finished)->Void in | |
if finished { | |
view.removeFromSuperview() | |
if let completion = objc_getAssociatedObject(view, &FloatingKeys.FKCompletion) as? CompletionWrapper { | |
completion.executeCompletion() | |
} | |
} | |
}) | |
} | |
//MARK: - Actions | |
@objc private func handleCloseButton(sender:UIButton) { | |
self.hideFloatingView(sender.superview!) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment