Skip to content

Instantly share code, notes, and snippets.

@alexeypegov
Created November 15, 2014 11:47
Show Gist options
  • Save alexeypegov/380e3e3af6f8e412987b to your computer and use it in GitHub Desktop.
Save alexeypegov/380e3e3af6f8e412987b to your computer and use it in GitHub Desktop.
SheetView
//
// SheetView.swift
// wrkts
//
// Created by Alexey Pegov on 28.10.14.
// Copyright (c) 2014 alexeypegov. All rights reserved.
//
import UIKit
class SheetView: UIView {
enum ViewState: UInt {
case Hidden = 1
case Showing
case Shown
case Hiding
}
let kAnimationDuration = 0.3
var state = ViewState.Hidden
var constraint: NSLayoutConstraint?
override init() {
super.init()
self.setTranslatesAutoresizingMaskIntoConstraints(false)
self.backgroundColor = UIColor.fromHex(0xFFFFFF)
self.layer.shadowOffset = CGSizeMake(0, 1)
self.layer.shadowColor = UIColor.blackColor().CGColor
self.layer.shadowRadius = 1.0
self.layer.shadowOpacity = 0.6
}
override init(frame: CGRect) {
super.init(frame: frame)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
func isVisible() -> Bool {
return state != ViewState.Hidden
}
func setupConstraintsWithNavigatinBar(bar: UINavigationBar) {
self.superview?.addConstraint(NSLayoutConstraint(item: self, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: bar, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0))
self.superview?.addConstraint(NSLayoutConstraint(item: self, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: bar, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0))
// self.superview?.addConstraint(NSLayoutConstraint(item: self, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
}
func constrainBehindNavigationBar(bar: UINavigationBar) {
if (constraint != nil) {
self.superview?.removeConstraint(constraint!)
}
constraint = NSLayoutConstraint(item: self, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: bar, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0)
self.superview?.addConstraint(constraint!)
}
func constrainBelowNavigationBar(bar: UINavigationBar) {
if (constraint != nil) {
self.superview?.removeConstraint(constraint!)
}
constraint = NSLayoutConstraint(item: self, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: bar, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0)
self.superview?.addConstraint(constraint!)
}
func showFromNavigationBar(bar: UINavigationBar, animated: Bool) {
if (state == ViewState.Shown || state == ViewState.Showing) {
self.hide(bar, animated: false)
}
bar.superview?.insertSubview(self, belowSubview: bar)
self.setupConstraintsWithNavigatinBar(bar)
if (animated && state == ViewState.Hidden) {
self.constrainBehindNavigationBar(bar)
}
self.superview?.layoutIfNeeded()
self.constrainBelowNavigationBar(bar);
let animations = { () -> Void in
self.state = ViewState.Showing
self.superview?.layoutIfNeeded()
}
let completion = { (finished: Bool) -> Void in
if (self.state == ViewState.Showing) {
self.state = ViewState.Shown
}
}
if (animated) {
UIView.animateWithDuration(kAnimationDuration, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions.CurveEaseOut, animations: animations, completion: completion)
} else {
animations()
completion(true)
}
}
func hide(bar: UINavigationBar, animated: Bool) {
if (state == ViewState.Hiding || state == ViewState.Hidden) {
return
}
self.constrainBehindNavigationBar(bar)
let animations = { () -> Void in
self.state = ViewState.Hiding
self.superview?.layoutIfNeeded()
}
let completion = { (finished: Bool) -> Void in
self.state = ViewState.Hidden
self.removeFromSuperview()
self.state = ViewState.Hidden
}
if (animated) {
UIView.animateWithDuration(kAnimationDuration, animations: animations, completion: completion)
} else {
animations()
completion(true)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment