Skip to content

Commit 1e44672

Browse files
author
zen
committed
Add state to BarView
1 parent 2ea8d66 commit 1e44672

6 files changed

Lines changed: 147 additions & 32 deletions

File tree

Example/Example/Base.lproj/Main.storyboard

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="6254" systemVersion="14B25" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="vXZ-lx-hvc">
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="6254" systemVersion="14B25" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="xG6-6Z-AjS">
33
<dependencies>
44
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6247"/>
55
</dependencies>
66
<scenes>
7+
<!--Navigation Controller-->
8+
<scene sceneID="K6i-du-ihS">
9+
<objects>
10+
<navigationController id="xG6-6Z-AjS" sceneMemberID="viewController">
11+
<navigationBar key="navigationBar" contentMode="scaleToFill" id="cF5-9S-tEB">
12+
<rect key="frame" x="0.0" y="0.0" width="320" height="44"/>
13+
<autoresizingMask key="autoresizingMask"/>
14+
</navigationBar>
15+
<connections>
16+
<segue destination="vXZ-lx-hvc" kind="relationship" relationship="rootViewController" id="VdL-8Y-qYn"/>
17+
</connections>
18+
</navigationController>
19+
<placeholder placeholderIdentifier="IBFirstResponder" id="CBk-bA-9J7" userLabel="First Responder" sceneMemberID="firstResponder"/>
20+
</objects>
21+
<point key="canvasLocation" x="-422" y="327"/>
22+
</scene>
723
<!--View Controller-->
824
<scene sceneID="ufC-wZ-h7g">
925
<objects>
@@ -29,6 +45,7 @@
2945
<constraint firstItem="2fi-mo-0CV" firstAttribute="top" secondItem="gqA-hQ-pSu" secondAttribute="bottom" id="cXG-3J-0ql"/>
3046
</constraints>
3147
</view>
48+
<navigationItem key="navigationItem" id="LlQ-s7-sQq"/>
3249
<connections>
3350
<outlet property="tableView" destination="gqA-hQ-pSu" id="YIY-hQ-Ext"/>
3451
</connections>

Persei.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
ADE974061A78F7AA0040B7C1 /* Persei.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = ADE973FA1A78F7AA0040B7C1 /* Persei.framework */; };
1212
ADE9740D1A78F7AA0040B7C1 /* PerseiTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = ADE9740C1A78F7AA0040B7C1 /* PerseiTests.swift */; };
1313
ADE974531A78F9ED0040B7C1 /* BarView.swift in Sources */ = {isa = PBXBuildFile; fileRef = ADE974521A78F9ED0040B7C1 /* BarView.swift */; };
14+
ADE974551A7916A90040B7C1 /* UIEdgeInsets+Equatable.swift in Sources */ = {isa = PBXBuildFile; fileRef = ADE974541A7916A90040B7C1 /* UIEdgeInsets+Equatable.swift */; };
15+
ADE974571A7940860040B7C1 /* UIScrollView+NormalizedContentOffset.swift in Sources */ = {isa = PBXBuildFile; fileRef = ADE974561A7940860040B7C1 /* UIScrollView+NormalizedContentOffset.swift */; };
1416
/* End PBXBuildFile section */
1517

1618
/* Begin PBXContainerItemProxy section */
@@ -31,6 +33,8 @@
3133
ADE9740B1A78F7AA0040B7C1 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
3234
ADE9740C1A78F7AA0040B7C1 /* PerseiTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PerseiTests.swift; sourceTree = "<group>"; };
3335
ADE974521A78F9ED0040B7C1 /* BarView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BarView.swift; sourceTree = "<group>"; };
36+
ADE974541A7916A90040B7C1 /* UIEdgeInsets+Equatable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIEdgeInsets+Equatable.swift"; sourceTree = "<group>"; };
37+
ADE974561A7940860040B7C1 /* UIScrollView+NormalizedContentOffset.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIScrollView+NormalizedContentOffset.swift"; sourceTree = "<group>"; };
3438
/* End PBXFileReference section */
3539

3640
/* Begin PBXFrameworksBuildPhase section */
@@ -75,6 +79,8 @@
7579
children = (
7680
ADE973FF1A78F7AA0040B7C1 /* Persei.h */,
7781
ADE974521A78F9ED0040B7C1 /* BarView.swift */,
82+
ADE974541A7916A90040B7C1 /* UIEdgeInsets+Equatable.swift */,
83+
ADE974561A7940860040B7C1 /* UIScrollView+NormalizedContentOffset.swift */,
7884
ADE973FD1A78F7AA0040B7C1 /* Supporting Files */,
7985
);
8086
path = Persei;
@@ -212,6 +218,8 @@
212218
isa = PBXSourcesBuildPhase;
213219
buildActionMask = 2147483647;
214220
files = (
221+
ADE974571A7940860040B7C1 /* UIScrollView+NormalizedContentOffset.swift in Sources */,
222+
ADE974551A7916A90040B7C1 /* UIEdgeInsets+Equatable.swift in Sources */,
215223
ADE974531A78F9ED0040B7C1 /* BarView.swift in Sources */,
216224
);
217225
runOnlyForDeploymentPostprocessing = 0;

Persei/BarView.swift

Lines changed: 64 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,38 @@ private var ContentOffsetContext = 0
1313

1414
public 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
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// UIEdgeInsets+Operators.swift
3+
// Persei
4+
//
5+
// Created by zen on 28/01/15.
6+
// Copyright (c) 2015 Yalantis. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
extension UIEdgeInsets: Equatable {
12+
}
13+
14+
public func == (lhs: UIEdgeInsets, rhs: UIEdgeInsets) -> Bool {
15+
return UIEdgeInsetsEqualToEdgeInsets(lhs, rhs)
16+
}
17+
18+
func + (lhs: UIEdgeInsets, rhs: UIEdgeInsets) -> UIEdgeInsets {
19+
var l: Int
20+
21+
return UIEdgeInsets(
22+
top: lhs.top + rhs.top,
23+
left: lhs.left + rhs.left,
24+
bottom: lhs.bottom + rhs.bottom,
25+
right: lhs.right + rhs.right
26+
)
27+
}
28+
29+
func - (lhs: UIEdgeInsets, rhs: UIEdgeInsets) -> UIEdgeInsets {
30+
return UIEdgeInsets(
31+
top: lhs.top - rhs.top,
32+
left: lhs.left - rhs.left,
33+
bottom: lhs.bottom - rhs.bottom,
34+
right: lhs.right - rhs.right
35+
)
36+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//
2+
// UIScrollView+NormalizedContentOffset.swift
3+
// Persei
4+
//
5+
// Created by zen on 28/01/15.
6+
// Copyright (c) 2015 Yalantis. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
extension UIScrollView {
12+
var normalizedContentOffset: CGPoint {
13+
get {
14+
15+
}
16+
17+
set {
18+
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)