@@ -13,15 +13,38 @@ private var ContentOffsetContext = 0
1313
1414public class BarView : UIView {
1515
16- // MARK: - Init
17- public override init ( frame: CGRect = CGRect ( x: 0.0 , y: - 70 .0, width: 320.0 , height: 70 .0) ) {
16+ // MARK: - Init
17+ public override init ( frame: CGRect = CGRect ( x: 0.0 , y: - 64 .0, width: 320.0 , height: 64 .0) ) {
1818 super. init ( frame: frame)
1919 }
2020
2121 public required init ( coder aDecoder: NSCoder ) {
2222 super. init ( coder: aDecoder)
2323 }
2424
25+ // MARK: - View lifecycle
26+ public override func willMoveToSuperview( newSuperview: UIView ? ) {
27+ super. willMoveToSuperview ( newSuperview)
28+ scrollView = nil
29+ }
30+
31+ public override func didMoveToSuperview( ) {
32+ super. didMoveToSuperview ( )
33+
34+ if superview != nil {
35+ scrollView = superview as UIScrollView
36+ }
37+ }
38+
39+ // MARK: - KVO
40+ public override func observeValueForKeyPath( keyPath: String , ofObject object: AnyObject , change: [ NSObject : AnyObject ] , context: UnsafeMutablePointer < Void > ) {
41+ if context == & ContentOffsetContext {
42+ didScroll ( scrollView. contentOffset)
43+ } else {
44+ super. observeValueForKeyPath ( keyPath, ofObject: object, change: change, context: context)
45+ }
46+ }
47+
2548 // MARK: - ScrollView
2649 private weak var scrollView : UIScrollView ! {
2750 willSet {
@@ -37,16 +60,42 @@ public class BarView: UIView {
3760 }
3861 }
3962
63+ // MARK: - State
64+ enum State {
65+ case Default, Revealed
66+ }
67+
68+ var state : State = . Default {
69+ didSet {
70+ if oldValue != state {
71+ switch state {
72+ case . Default:
73+ self . addInsets ( )
74+
75+ case . Revealed:
76+ self . removeInsets ( )
77+ }
78+ }
79+ }
80+ }
81+
4082 // MARK: - Applyied Insets
4183 private var appliedInsets : UIEdgeInsets = UIEdgeInsetsZero
4284 private var applyingInsets = false
43-
85+ private var insetsApplied : Bool {
86+ return !applyingInsets && appliedInsets != UIEdgeInsetsZero
87+ }
88+
4489 private func applyInsets( insets: UIEdgeInsets , animated: Bool ) {
4590 appliedInsets = insets
4691
4792 if animated {
4893 applyingInsets = true
49- UIView . animateWithDuration ( 0.2 , animations: {
94+
95+ let curve : UIViewAnimationOptions = scrollView. dragging ? . CurveEaseOut : . CurveEaseInOut
96+ let options : UIViewAnimationOptions = . BeginFromCurrentState
97+
98+ UIView . animateWithDuration ( 0.2 , delay: 0.0 , options: . BeginFromCurrentState | curve, animations: {
5099 self . scrollView. contentInset = self . appliedInsets
51100 } , completion: { completed in
52101 self . applyingInsets = false
@@ -57,16 +106,21 @@ public class BarView: UIView {
57106 }
58107
59108 private func addInsets( animated: Bool = true ) {
60- applyInsets ( UIEdgeInsets ( top: 70.0 , left: 0.0 , bottom: 0.0 , right: 0.0 ) , animated: animated)
109+ assert ( !insetsApplied, " Internal inconsistency " )
110+ applyInsets ( UIEdgeInsets ( top: 64.0 , left: 0.0 , bottom: 0.0 , right: 0.0 ) , animated: animated)
61111 }
62112
63113 private func removeInsets( animated: Bool = true ) {
114+ assert ( insetsApplied, " Internal inconsistency " )
64115 applyInsets ( UIEdgeInsetsZero, animated: animated)
65116 }
66117
67118 // MARK: - Content Offset Hanlding
68- private func didScroll( offset: CGPoint ) {
69- frame = CGRect ( x: 0.0 , y: 0.0 , width: CGRectGetWidth ( scrollView. bounds) , height: - 70.0 )
119+ private func didScroll( var offset: CGPoint ) {
120+ offset = CGPoint ( x: scrollView. contentOffset. x, y: scrollView. contentOffset. y + appliedInsets. top)
121+ println ( offset)
122+
123+ frame = CGRect ( x: 0.0 , y: 0.0 , width: CGRectGetWidth ( scrollView. bounds) , height: - 64.0 )
70124 }
71125
72126 @objc
@@ -77,7 +131,9 @@ public class BarView: UIView {
77131
78132 switch recognizer. state {
79133 case . Ended:
80- let insetsApplied = !UIEdgeInsetsEqualToEdgeInsets( appliedInsets, UIEdgeInsetsZero)
134+
135+
136+ let insetsApplied = appliedInsets != UIEdgeInsetsZero
81137 let offset = scrollView. contentOffset
82138
83139 if offset. y < 0.0 && !insetsApplied && scrollView. panGestureRecognizer. state != . Changed {
@@ -90,27 +146,4 @@ public class BarView: UIView {
90146 break
91147 }
92148 }
93-
94- // MARK: - View lifecycle
95- public override func willMoveToSuperview( newSuperview: UIView ? ) {
96- super. willMoveToSuperview ( newSuperview)
97- scrollView = nil
98- }
99-
100- public override func didMoveToSuperview( ) {
101- super. didMoveToSuperview ( )
102-
103- if superview != nil {
104- scrollView = superview as UIScrollView
105- }
106- }
107-
108- // MARK: - KVO
109- public override func observeValueForKeyPath( keyPath: String , ofObject object: AnyObject , change: [ NSObject : AnyObject ] , context: UnsafeMutablePointer < Void > ) {
110- if context == & ContentOffsetContext {
111- didScroll ( scrollView. contentOffset)
112- } else {
113- super. observeValueForKeyPath ( keyPath, ofObject: object, change: change, context: context)
114- }
115- }
116149}
0 commit comments