Skip to content

Commit

Permalink
feat: add gradient to cards in PoodleSurf iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
n8chur authored and roperzh committed Nov 14, 2019
1 parent e9a3d6b commit dd8eeab
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 4 deletions.
12 changes: 12 additions & 0 deletions examples/poodle-surf/ios/PoodleSurf.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
46087C3B225FDA8800F4B63F /* NSLayoutConstraint+Named.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46087C3A225FDA8800F4B63F /* NSLayoutConstraint+Named.swift */; };
46087C41225FF41600F4B63F /* loading-pizza.json in Resources */ = {isa = PBXBuildFile; fileRef = 46087C40225FF41600F4B63F /* loading-pizza.json */; };
46087C43225FF46D00F4B63F /* surfing-poodle.json in Resources */ = {isa = PBXBuildFile; fileRef = 46087C42225FF46D00F4B63F /* surfing-poodle.json */; };
46087C45225FF79C00F4B63F /* GradientView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46087C44225FF79C00F4B63F /* GradientView.swift */; };
46087C47225FFC1800F4B63F /* Gradient.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46087C46225FFC1800F4B63F /* Gradient.swift */; };
46087C49225FFC3000F4B63F /* Gradient+Example.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46087C48225FFC3000F4B63F /* Gradient+Example.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand Down Expand Up @@ -62,6 +65,9 @@
46087C3A225FDA8800F4B63F /* NSLayoutConstraint+Named.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSLayoutConstraint+Named.swift"; sourceTree = "<group>"; };
46087C40225FF41600F4B63F /* loading-pizza.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "loading-pizza.json"; sourceTree = "<group>"; };
46087C42225FF46D00F4B63F /* surfing-poodle.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "surfing-poodle.json"; sourceTree = "<group>"; };
46087C44225FF79C00F4B63F /* GradientView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GradientView.swift; sourceTree = "<group>"; };
46087C46225FFC1800F4B63F /* Gradient.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Gradient.swift; sourceTree = "<group>"; };
46087C48225FFC3000F4B63F /* Gradient+Example.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Gradient+Example.swift"; sourceTree = "<group>"; };
6C4AECABD9C1727831BEA573 /* Pods_PoodleSurf.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_PoodleSurf.framework; sourceTree = BUILT_PRODUCTS_DIR; };
8E08F93612D7318296206E7F /* Pods-PoodleSurf.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-PoodleSurf.release.xcconfig"; path = "Target Support Files/Pods-PoodleSurf/Pods-PoodleSurf.release.xcconfig"; sourceTree = "<group>"; };
/* End PBXFileReference section */
Expand Down Expand Up @@ -127,6 +133,9 @@
46087C0B225D5F0B00F4B63F /* HorizontalImageLabelView.swift */,
46087C0D225D62CF00F4B63F /* HorizontalImageVerticalLabelsView.swift */,
46087BFE225D544400F4B63F /* StrokedCircularImageView.swift */,
46087C44225FF79C00F4B63F /* GradientView.swift */,
46087C46225FFC1800F4B63F /* Gradient.swift */,
46087C48225FFC3000F4B63F /* Gradient+Example.swift */,
);
path = "Generic Views";
sourceTree = "<group>";
Expand Down Expand Up @@ -347,8 +356,11 @@
46087C39225FD3F500F4B63F /* CardViewDescribable.swift in Sources */,
46087C14225D795000F4B63F /* LoadingViewController.swift in Sources */,
46087BE7225D341600F4B63F /* AppDelegate.swift in Sources */,
46087C45225FF79C00F4B63F /* GradientView.swift in Sources */,
46087C3B225FDA8800F4B63F /* NSLayoutConstraint+Named.swift in Sources */,
46087C47225FFC1800F4B63F /* Gradient.swift in Sources */,
46087C35225FCEF000F4B63F /* UIImage+CardinalDirection.swift in Sources */,
46087C49225FFC3000F4B63F /* Gradient+Example.swift in Sources */,
46087BFA225D3F9900F4B63F /* ReportView.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
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 examples/poodle-surf/ios/PoodleSurf/Generic Views/Gradient.swift
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)
}
}
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.") }
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class LoadingViewController: UIViewController {
}

private func applyStyle(to view: LoadingView) {
view.backgroundColor = UIColor(red: 120.0/255.0, green: 207.0/255.0, blue: 253.0/255.0, alpha: 1)
view.backgroundColor = UIColor(red: 120/255, green: 207/255, blue: 253/255, alpha: 1)
view.animationView.setAnimation(named: "loading-pizza")
view.animationView.play()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ protocol CardViewDescribable: UIView {
var titleLabel: UILabel { get }
var layoutMargins: UIEdgeInsets { get }
var titleContentSpacing: CGFloat { get set }
var gradient: Gradient? { get set }
}

extension ForecastCardView: CardViewDescribable { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ class ForecastCardView: UIView {
set { separatorWidthConstraints.forEach { $0.constant = newValue } }
}

var gradient: Gradient? {
get { return gradientView.gradient }
set { gradientView.gradient = newValue }
}

func setDayPartViewsVerticalSpacing(_ spacing: CGFloat) {
dayParts.forEach { $0.verticalSpacing = spacing }
}
Expand All @@ -77,9 +82,12 @@ class ForecastCardView: UIView {
private let outterStackView: UIStackView
private let partsStackView: UIStackView
private var separatorWidthConstraints: [NSLayoutConstraint] = []
private let gradientView = GradientView()

private func setupLayout() {
var constraints = embed(outterStackView, shouldActivateConstraints: false)
var constraints = embed(gradientView, shouldActivateConstraints: false)

constraints += embed(outterStackView, shouldActivateConstraints: false)

constraints += addSeparators()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,23 @@ class TemperatureCardView: UIView {
set { outterStackView.layoutMargins = newValue }
}

var gradient: Gradient? {
get { return gradientView.gradient }
set { gradientView.gradient = newValue }
}

override class var requiresConstraintBasedLayout: Bool { return true }

private let outterStackView: UIStackView
private let innerStackView: UIStackView
private let gradientView = GradientView()

private func setupLayout() {
embed(outterStackView)
var constraints = embed(gradientView, shouldActivateConstraints: false)

constraints += embed(outterStackView, shouldActivateConstraints: false)

NSLayoutConstraint.activate(constraints)
}

private func configureViews() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,12 @@ class ReportViewController: UIViewController {
}

private func applyCardStyle(to view: CardViewDescribable) {
view.backgroundColor = UIColor(red: 0.98, green: 0.35, blue: 0.4, alpha: 1)
view.titleLabel.font = .systemFont(ofSize: 20)
view.titleLabel.textColor = .white
view.layoutMargins = UIEdgeInsets(top: 20, left: 20, bottom: 30, right: 20)
view.cornerRadius = 5
view.titleContentSpacing = 20
view.gradient = Gradient.makeExample()
}

private func applyTitleStyle(to view: HorizontalImageLabelView) {
Expand Down

0 comments on commit dd8eeab

Please sign in to comment.