-
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add gradient to cards in PoodleSurf iOS
- Loading branch information
Showing
9 changed files
with
140 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
19 changes: 19 additions & 0 deletions
19
examples/poodle-surf/ios/PoodleSurf/Generic Views/Gradient+Example.swift
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// | ||
// Gradient+Example.swift | ||
// PoodleSurf | ||
// | ||
// Created by Westin Newell on 4/11/19. | ||
// Copyright © 2019 Haiku. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
|
||
extension Gradient { | ||
static func makeExample() -> Gradient { | ||
return Gradient( | ||
startColor: UIColor(red: 255/255, green: 63/255, blue: 112/255, alpha: 1), | ||
endColor: UIColor(red: 255/255, green: 154/255, blue: 58/255, alpha: 1), | ||
startPoint: GradientPoint(x: 0, y: 0), | ||
endPoint: GradientPoint(x: 1, y: 1)) | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
examples/poodle-surf/ios/PoodleSurf/Generic Views/Gradient.swift
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// | ||
// Gradient.swift | ||
// PoodleSurf | ||
// | ||
// Created by Westin Newell on 4/11/19. | ||
// Copyright © 2019 Haiku. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
|
||
struct Gradient { | ||
struct ColorPosition { | ||
let color: UIColor | ||
/// A value from 0-1 that represents the gradient color's position. | ||
let location: Double | ||
} | ||
|
||
/// A point in a coordinate space that starts at the top left (0,0) and ends at the bottom right (1, 1). | ||
typealias GradientPoint = CGPoint | ||
|
||
let colors: [ColorPosition] | ||
let startPoint: GradientPoint | ||
let endPoint: GradientPoint | ||
|
||
init(colors: [ColorPosition], startPoint: GradientPoint, endPoint: GradientPoint) { | ||
self.colors = colors | ||
self.startPoint = startPoint | ||
self.endPoint = endPoint | ||
} | ||
|
||
init(startColor: UIColor, endColor: UIColor, startPoint: GradientPoint, endPoint: GradientPoint) { | ||
let colors = [ | ||
ColorPosition(color: startColor, location: 0), | ||
ColorPosition(color: endColor, location: 1), | ||
] | ||
|
||
self.init(colors: colors, startPoint: startPoint, endPoint: endPoint) | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
examples/poodle-surf/ios/PoodleSurf/Generic Views/GradientView.swift
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// | ||
// Gradient.swift | ||
// PoodleSurf | ||
// | ||
// Created by Westin Newell on 4/11/19. | ||
// Copyright © 2019 Haiku. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
import CoreGraphics | ||
|
||
class GradientView: UIView { | ||
var gradient: Gradient? { | ||
didSet { | ||
guard let gradient = gradient else { | ||
gradientLayer.removeFromSuperlayer() | ||
return | ||
} | ||
|
||
gradientLayer.colors = gradient.colors.map { $0.color.cgColor } | ||
gradientLayer.locations = gradient.colors.map { NSNumber(floatLiteral: $0.location) } | ||
gradientLayer.startPoint = gradient.startPoint | ||
gradientLayer.endPoint = gradient.endPoint | ||
|
||
if gradientLayer.superlayer == nil { | ||
layer.addSublayer(gradientLayer) | ||
} | ||
} | ||
} | ||
|
||
override init(frame: CGRect) { | ||
super.init(frame: frame) | ||
|
||
gradientLayer.frame = layer.bounds | ||
} | ||
|
||
override func layoutSubviews() { | ||
super.layoutSubviews() | ||
|
||
gradientLayer.frame = layer.bounds | ||
} | ||
|
||
private let gradientLayer = CAGradientLayer() | ||
|
||
@available(*, unavailable) | ||
required init?(coder aDecoder: NSCoder) { fatalError("\(#function) not implemented.") } | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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