Skip to content

Commit

Permalink
Add demo for radial gradients
Browse files Browse the repository at this point in the history
  • Loading branch information
tbaranes committed Jan 8, 2018
1 parent d46b8c1 commit b19942e
Show file tree
Hide file tree
Showing 7 changed files with 673 additions and 304 deletions.
22 changes: 19 additions & 3 deletions IBAnimatableApp/IBAnimatableApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
objects = {

/* Begin PBXBuildFile section */
E20DAEBD20037C5E00BE1C88 /* UserInterfaceGradients.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = E20DAEBC20037C5E00BE1C88 /* UserInterfaceGradients.storyboard */; };
E20DAEBF20037C7800BE1C88 /* UserInterfaceGradientsTableViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = E20DAEBE20037C7800BE1C88 /* UserInterfaceGradientsTableViewController.swift */; };
E2307A311ECD81D6000E38B2 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = E2307A2B1ECD81D6000E38B2 /* Assets.xcassets */; };
E2307A331ECD81D6000E38B2 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = E2307A2E1ECD81D6000E38B2 /* LaunchScreen.storyboard */; };
E2307A621ECD8278000E38B2 /* DemoApp.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = E2307A381ECD8278000E38B2 /* DemoApp.storyboard */; };
Expand Down Expand Up @@ -65,6 +67,8 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
E20DAEBC20037C5E00BE1C88 /* UserInterfaceGradients.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = UserInterfaceGradients.storyboard; sourceTree = "<group>"; };
E20DAEBE20037C7800BE1C88 /* UserInterfaceGradientsTableViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UserInterfaceGradientsTableViewController.swift; sourceTree = "<group>"; };
E2307A151ECD7F81000E38B2 /* IBAnimatableApp.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = IBAnimatableApp.app; sourceTree = BUILT_PRODUCTS_DIR; };
E2307A2B1ECD81D6000E38B2 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
E2307A2F1ECD81D6000E38B2 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
Expand Down Expand Up @@ -121,6 +125,17 @@
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
E20DAEBB20037C4200BE1C88 /* Gradients */ = {
isa = PBXGroup;
children = (
E20DAEBC20037C5E00BE1C88 /* UserInterfaceGradients.storyboard */,
E20DAEBE20037C7800BE1C88 /* UserInterfaceGradientsTableViewController.swift */,
E2A5476D1ECF1C4400842C1F /* GradientViewController.swift */,
E2A5476C1ECF1C4400842C1F /* GradientCustomStartPointViewController.swift */,
);
path = Gradients;
sourceTree = "<group>";
};
E2307A0C1ECD7F81000E38B2 = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -254,13 +269,12 @@
isa = PBXGroup;
children = (
E2A5476E1ECF1C4400842C1F /* Mask */,
E20DAEBB20037C4200BE1C88 /* Gradients */,
E2A547711ECF1C4400842C1F /* UserInterface.storyboard */,
E2A547721ECF1C4400842C1F /* UserInterfaceTableViewController.swift */,
E2A547691ECF1C4400842C1F /* BlurEffectViewController.swift */,
E2A5476A1ECF1C4400842C1F /* BorderViewController.swift */,
E2A5476B1ECF1C4400842C1F /* CornerViewController.swift */,
E2A5476C1ECF1C4400842C1F /* GradientCustomStartPointViewController.swift */,
E2A5476D1ECF1C4400842C1F /* GradientViewController.swift */,
E2A547721ECF1C4400842C1F /* UserInterfaceTableViewController.swift */,
);
path = UserInterfaces;
sourceTree = "<group>";
Expand Down Expand Up @@ -362,6 +376,7 @@
E2A5477A1ECF1C4400842C1F /* UserInterface.storyboard in Resources */,
E2307A311ECD81D6000E38B2 /* Assets.xcassets in Resources */,
E2A5477F1ECF1C4E00842C1F /* ActivityIndicators.storyboard in Resources */,
E20DAEBD20037C5E00BE1C88 /* UserInterfaceGradients.storyboard in Resources */,
E2307A8B1ECD8736000E38B2 /* Main.storyboard in Resources */,
E2A547371ECF1C0E00842C1F /* Presentations.storyboard in Resources */,
E2307A621ECD8278000E38B2 /* DemoApp.storyboard in Resources */,
Expand Down Expand Up @@ -408,6 +423,7 @@
E2A5474C1ECF1C0E00842C1F /* Functions.swift in Sources */,
E2A547301ECF1C0E00842C1F /* AnimationsViewController.swift in Sources */,
E2A547341ECF1C0E00842C1F /* InteractionTableViewController.swift in Sources */,
E20DAEBF20037C7800BE1C88 /* UserInterfaceGradientsTableViewController.swift in Sources */,
E2A5473B1ECF1C0E00842C1F /* RefreshControlTableViewController.swift in Sources */,
E2A547761ECF1C4400842C1F /* GradientCustomStartPointViewController.swift in Sources */,
E2A547381ECF1C0E00842C1F /* PresentingViewController.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//
// GradientCustomStartPointViewController.swift
// IBAnimatable
//
// Created by Tom Baranes on 22/12/2016.
// Copyright © 2016 IBAnimatable. All rights reserved.
//

import UIKit
import IBAnimatable

final class GradientCustomStartPointViewController: UIViewController {

@IBOutlet fileprivate weak var gView: AnimatableView!

var useRadialGradient = false
let gradientValues = ParamType(fromEnum: GradientType.self)
let coordPointValues = ParamType.number(min: 0, max: 1, interval: 0.1, ascending: true, unit: "")
lazy var componentValues: [ParamType] = [self.gradientValues,
self.coordPointValues,
self.coordPointValues,
self.coordPointValues,
self.coordPointValues]

override func viewDidLoad() {
super.viewDidLoad()
gView.gradientMode = useRadialGradient ? .radial : .linear
gView.predefinedGradient = GradientType(rawValue: gradientValues.value(at: 0))
}
}

extension GradientCustomStartPointViewController: UIPickerViewDelegate, UIPickerViewDataSource {
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return componentValues[component].count()
}

func numberOfComponents(in pickerView: UIPickerView) -> Int {
return componentValues.count
}
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
let label = UILabel()
label.textColor = .white
label.textAlignment = .center
label.minimumScaleFactor = 0.5
label.text = componentValues[component].title(at: row)
return label
}
func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
return componentValues[component].title(at: row).colorize(.white)
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
let startX = pickerView.selectedRow(inComponent: 1)
let startY = pickerView.selectedRow(inComponent: 2)
let endX = pickerView.selectedRow(inComponent: 3)
let endY = pickerView.selectedRow(inComponent: 4)
let startPoint = CGPoint(x: Double(coordPointValues.value(at: startX)) ?? 0, y: Double(coordPointValues.value(at: startY)) ?? 0)
let endPoint = CGPoint(x: Double(coordPointValues.value(at: endX)) ?? 0, y: Double(coordPointValues.value(at: endY)) ?? 0)

gView.startPoint = .custom(start: startPoint, end: endPoint)
gView.predefinedGradient = GradientType(rawValue: gradientValues.value(at: pickerView.selectedRow(inComponent: 0)))
gView.configureGradient()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
//
// Created by jason akakpo on 27/07/16.
// Copyright © 2016 IBAnimatable. All rights reserved.
//

import UIKit
import IBAnimatable

final class GradientViewController: UIViewController {

@IBOutlet fileprivate weak var gView: AnimatableView!

let gradientValues = ParamType(fromEnum: GradientType.self)
let startPointValues = ParamType.enumeration(values: ["top", "topLeft", "topRight", "left", "right", "bottom", "bottomRight", "bottomLeft"])
let colorValues = ParamType(fromEnum: ColorType.self)
var usePredefinedGradient = true
var useRadialGradient = false
lazy var componentValues: [ParamType] = {
self.usePredefinedGradient ? [self.gradientValues, self.startPointValues] : [self.colorValues, self.colorValues, self.startPointValues]
}()

override func viewDidLoad() {
super.viewDidLoad()
gView.gradientMode = useRadialGradient ? .radial : .linear
if usePredefinedGradient {
gView.predefinedGradient = GradientType(rawValue: gradientValues.value(at: 0))
gView.startPoint = GradientStartPoint(string: startPointValues.value(at: 0))
} else {
gView.startColor = ColorType(rawValue: self.colorValues.value(at: 0))?.color
gView.endColor = ColorType(rawValue: self.colorValues.value(at: 0))?.color
}
let navigationBar = self.navigationController?.navigationBar as? DesignableNavigationBar
navigationBar?.copyGradient(from: gView)
navigationBar?.configureGradient()
}

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
let navigationBar = self.navigationController?.navigationBar as? DesignableNavigationBar
navigationBar?.resetGradient()
navigationBar?.setBackgroundImage(nil, for: .default)
}

}

extension GradientViewController: UIPickerViewDelegate, UIPickerViewDataSource {
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return componentValues[component].count()
}

func numberOfComponents(in pickerView: UIPickerView) -> Int {
return componentValues.count
}
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
let label = UILabel()
label.textColor = .white
label.textAlignment = .center
label.minimumScaleFactor = 0.5
label.text = componentValues[component].title(at: row)
return label
}
func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
return componentValues[component].title(at: row).colorize(.white)
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if usePredefinedGradient {
gView.predefinedGradient = GradientType(rawValue: gradientValues.value(at: pickerView.selectedRow(inComponent: 0)))
gView.startPoint = GradientStartPoint(string: startPointValues.value(at: pickerView.selectedRow(inComponent: 1)))
} else {
gView.startColor = ColorType(rawValue: self.colorValues.value(at: pickerView.selectedRow(inComponent: 0)))?.color
gView.endColor = ColorType(rawValue: self.colorValues.value(at: pickerView.selectedRow(inComponent: 1)))?.color
gView.startPoint = GradientStartPoint(string: startPointValues.value(at: pickerView.selectedRow(inComponent: 2)))
}
gView.configureGradient()

let navigationBar = self.navigationController?.navigationBar as? DesignableNavigationBar
navigationBar?.copyGradient(from: gView)
navigationBar?.configureGradient()
}
}

fileprivate extension GradientDesignable {

func copyGradient(from designable: GradientDesignable) {
predefinedGradient = designable.predefinedGradient
startColor = designable.startColor
endColor = designable.endColor
startPoint = designable.startPoint
}

func resetGradient(defaultStartPoint: GradientStartPoint = .top) {
predefinedGradient = nil
startColor = nil
endColor = nil
startPoint = defaultStartPoint
}

}
Loading

0 comments on commit b19942e

Please sign in to comment.