Skip to content

Commit 9090117

Browse files
committed
Add shadow and overlay
1 parent dadb54b commit 9090117

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

Demo/GearAnimation.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
654E94251ABAC1CE007CDCD8 /* ShadowView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 654E94241ABAC1CE007CDCD8 /* ShadowView.swift */; };
1011
65802E821AB9677200E40510 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 65802E811AB9677200E40510 /* AppDelegate.swift */; };
1112
65802E841AB9677200E40510 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 65802E831AB9677200E40510 /* ViewController.swift */; };
1213
65802E871AB9677200E40510 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 65802E851AB9677200E40510 /* Main.storyboard */; };
@@ -28,6 +29,7 @@
2829
/* End PBXContainerItemProxy section */
2930

3031
/* Begin PBXFileReference section */
32+
654E94241ABAC1CE007CDCD8 /* ShadowView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ShadowView.swift; sourceTree = "<group>"; };
3133
65802E7C1AB9677200E40510 /* GearAnimation.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = GearAnimation.app; sourceTree = BUILT_PRODUCTS_DIR; };
3234
65802E801AB9677200E40510 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
3335
65802E811AB9677200E40510 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
@@ -122,6 +124,7 @@
122124
children = (
123125
65802EA21AB97EFE00E40510 /* MainGear.swift */,
124126
65802EA41AB988F800E40510 /* BigGear.swift */,
127+
654E94241ABAC1CE007CDCD8 /* ShadowView.swift */,
125128
);
126129
path = Views;
127130
sourceTree = "<group>";
@@ -230,6 +233,7 @@
230233
65802EA51AB988F800E40510 /* BigGear.swift in Sources */,
231234
65802E841AB9677200E40510 /* ViewController.swift in Sources */,
232235
65802E821AB9677200E40510 /* AppDelegate.swift in Sources */,
236+
654E94251ABAC1CE007CDCD8 /* ShadowView.swift in Sources */,
233237
);
234238
runOnlyForDeploymentPostprocessing = 0;
235239
};
@@ -421,6 +425,7 @@
421425
65802E9D1AB9677300E40510 /* Release */,
422426
);
423427
defaultConfigurationIsVisible = 0;
428+
defaultConfigurationName = Release;
424429
};
425430
65802E9E1AB9677300E40510 /* Build configuration list for PBXNativeTarget "GearAnimationTests" */ = {
426431
isa = XCConfigurationList;
@@ -429,6 +434,7 @@
429434
65802EA01AB9677300E40510 /* Release */,
430435
);
431436
defaultConfigurationIsVisible = 0;
437+
defaultConfigurationName = Release;
432438
};
433439
/* End XCConfigurationList section */
434440
};

Demo/GearAnimation/ViewController.swift

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import UIKit
1111
class ViewController: UITableViewController {
1212

1313
var refreshContainerView: UIView!
14+
var overlayView: UIView!
1415
var isRefreshControlAnimating = false
16+
var shadowView: ShadowView!
1517
var centerGear: MainGear!
1618
var topGear: BigGear! // cue Jessica from the Allman brothers
1719
var rightGear: BigGear!
@@ -39,6 +41,12 @@ class ViewController: UITableViewController {
3941
self.refreshContainerView = UIView(frame: self.refreshControl!.bounds)
4042
self.refreshContainerView.backgroundColor = UIColor(red:0.13, green:0.29, blue:0.55, alpha:1)
4143

44+
self.overlayView = UIView(frame: self.refreshControl!.bounds)
45+
self.overlayView.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.3)
46+
47+
self.shadowView = ShadowView(frame: self.refreshControl!.bounds)
48+
self.shadowView.shadowPercentage = 0.2
49+
4250
self.centerGear = MainGear(frame: CGRectMake(0, 0, 48, 48))
4351
self.centerGear.backgroundColor = UIColor.clearColor()
4452
self.centerGear.tintColor = UIColor(red:0.36, green:0.52, blue:0.92, alpha:1)
@@ -69,6 +77,8 @@ class ViewController: UITableViewController {
6977
self.refreshContainerView.addSubview(self.bottomGear)
7078
self.refreshContainerView.addSubview(self.leftGear)
7179
self.refreshContainerView.addSubview(self.centerGear)
80+
self.refreshContainerView.addSubview(self.shadowView)
81+
self.refreshContainerView.addSubview(self.overlayView)
7282

7383
self.refreshContainerView.clipsToBounds = true
7484
self.refreshControl!.tintColor = UIColor.clearColor()
@@ -92,6 +102,8 @@ class ViewController: UITableViewController {
92102
var pullDistance = max(0.0, -self.refreshControl!.frame.origin.y);
93103
var pullRatio = min(max(pullDistance, 0.0), 140.0) / 140.0;
94104

105+
self.overlayView.alpha = 1 - pullRatio
106+
95107
self.centerGear.center = CGPoint(x: CGRectGetMidX(self.refreshContainerView.frame), y: pullDistance / 2)
96108
self.topGear.center = CGPoint(x: CGRectGetMidX(self.refreshContainerView.frame) + 48, y: pullDistance / 2 - 49)
97109
self.rightGear.center = CGPoint(x: CGRectGetMidX(self.refreshContainerView.frame) + 120, y: pullDistance / 2)
@@ -100,7 +112,7 @@ class ViewController: UITableViewController {
100112

101113
refreshBounds.size.height = pullDistance;
102114

103-
self.refreshContainerView.frame = refreshBounds;
115+
[self.refreshContainerView, self.overlayView].map({$0.frame = refreshBounds});
104116

105117
// Don't rotate the gears if the refresh animation is playing
106118
if (!self.refreshControl!.refreshing && !self.isRefreshControlAnimating) {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import UIKit
2+
3+
class ShadowView: UIView {
4+
var gradient: CAGradientLayer!
5+
var shadowPercentage = 0.0
6+
7+
override func layoutSubviews() {
8+
self.clipsToBounds = true
9+
if (self.gradient == nil) {
10+
self.gradient = CAGradientLayer()
11+
var frame = self.frame
12+
let offset = frame.size.height * CGFloat(self.shadowPercentage)
13+
frame.size.height = offset
14+
self.gradient.frame = frame
15+
self.gradient.colors = [
16+
UIColor.blackColor().colorWithAlphaComponent(0.6).CGColor,
17+
UIColor.blackColor().colorWithAlphaComponent(0.15).CGColor,
18+
UIColor.clearColor().CGColor]
19+
self.layer.insertSublayer(self.gradient, atIndex:0)
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)