Created
August 29, 2016 11:40
-
-
Save hamishknight/e44bd25823d24dea068e0498fa1cef4b 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
import UIKit | |
struct ConstraintInfo { | |
var attribute: NSLayoutAttribute = .left | |
var secondAttribute: NSLayoutAttribute = .notAnAttribute | |
var constant: CGFloat = 0 | |
var identifier: String? | |
var relation: NSLayoutRelation = .equal | |
} | |
precedencegroup constPrecedence { | |
associativity: left | |
higherThan: AssignmentPrecedence | |
} | |
infix operator >>>- : constPrecedence | |
@discardableResult | |
func >>>- <T: UIView> (left: (T, T), block: (inout ConstraintInfo) -> ()) -> NSLayoutConstraint { | |
var info = ConstraintInfo() | |
block(&info) | |
info.secondAttribute = info.secondAttribute == .notAnAttribute ? info.attribute : info.secondAttribute | |
let constraint = NSLayoutConstraint(item: left.1, | |
attribute: info.attribute, | |
relatedBy: info.relation, | |
toItem: left.0, | |
attribute: info.secondAttribute, | |
multiplier: 1, | |
constant: info.constant) | |
constraint.identifier = info.identifier | |
left.0.addConstraint(constraint) | |
return constraint | |
} | |
protocol GestureControlDelegate { | |
func gestureControlDidSwipe(direction: UISwipeGestureRecognizerDirection) | |
} | |
class GestureControl: UIView { | |
let delegate: GestureControlDelegate | |
init(view: UIView, delegate: GestureControlDelegate) { | |
self.delegate = delegate | |
super.init(frame: CGRect.zero) | |
let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(swipeHandler(gesture:))) | |
swipeLeft.direction = .left | |
addGestureRecognizer(swipeLeft) | |
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(swipeHandler(gesture:))) | |
swipeRight.direction = .right | |
addGestureRecognizer(swipeRight) | |
translatesAutoresizingMaskIntoConstraints = false | |
backgroundColor = .clear | |
view.addSubview(self) | |
for attribute: NSLayoutAttribute in [.left, .right, .top, .bottom] { | |
(view, self) >>>- { | |
$0.attribute = attribute | |
} | |
} | |
} | |
required init?(coder aDecoder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
} | |
extension GestureControl { | |
dynamic func swipeHandler(gesture: UISwipeGestureRecognizer) { | |
delegate.gestureControlDidSwipe(direction: gesture.direction) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment