Skip to content

Commit

Permalink
Enable changing of pinning and layoutMargin options
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric Hyche committed Aug 1, 2017
1 parent da4ae0c commit 1d51266
Show file tree
Hide file tree
Showing 7 changed files with 346 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
FD07155F1F300A6300F7CB7A /* UILayoutConstraintAxis+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = FD07155E1F300A6300F7CB7A /* UILayoutConstraintAxis+Extensions.swift */; };
FD0715611F300F4100F7CB7A /* UIStackViewDistribution+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = FD0715601F300F4100F7CB7A /* UIStackViewDistribution+Extensions.swift */; };
FD0715631F3011F100F7CB7A /* UIStackViewAlignment+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = FD0715621F3011F100F7CB7A /* UIStackViewAlignment+Extensions.swift */; };
FD0715651F3027D400F7CB7A /* CGFloat+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = FD0715641F3027D400F7CB7A /* CGFloat+Extensions.swift */; };
FD96E4201F30DF810008DEE0 /* Bool+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = FD96E41F1F30DF810008DEE0 /* Bool+Extensions.swift */; };
FD96E4221F30F3450008DEE0 /* EHPinningOptions.swift in Sources */ = {isa = PBXBuildFile; fileRef = FD96E4211F30F3450008DEE0 /* EHPinningOptions.swift */; };
FDD291411F2E18C2009CC565 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = FDD291401F2E18C2009CC565 /* AppDelegate.swift */; };
FDD291431F2E18C2009CC565 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = FDD291421F2E18C2009CC565 /* ViewController.swift */; };
FDD291461F2E18C2009CC565 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = FDD291441F2E18C2009CC565 /* Main.storyboard */; };
Expand Down Expand Up @@ -38,6 +41,9 @@
FD07155E1F300A6300F7CB7A /* UILayoutConstraintAxis+Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UILayoutConstraintAxis+Extensions.swift"; sourceTree = "<group>"; };
FD0715601F300F4100F7CB7A /* UIStackViewDistribution+Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIStackViewDistribution+Extensions.swift"; sourceTree = "<group>"; };
FD0715621F3011F100F7CB7A /* UIStackViewAlignment+Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIStackViewAlignment+Extensions.swift"; sourceTree = "<group>"; };
FD0715641F3027D400F7CB7A /* CGFloat+Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "CGFloat+Extensions.swift"; sourceTree = "<group>"; };
FD96E41F1F30DF810008DEE0 /* Bool+Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Bool+Extensions.swift"; sourceTree = "<group>"; };
FD96E4211F30F3450008DEE0 /* EHPinningOptions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EHPinningOptions.swift; sourceTree = "<group>"; };
FDD2913D1F2E18C2009CC565 /* LearnAboutStackViews.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = LearnAboutStackViews.app; sourceTree = BUILT_PRODUCTS_DIR; };
FDD291401F2E18C2009CC565 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
FDD291421F2E18C2009CC565 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -104,6 +110,7 @@
FDD2916F1F2E3656009CC565 /* EHStackViewSettingsViewController.swift */,
FDD291741F2E7060009CC565 /* EHStackViewSettingsModel.swift */,
FD07155C1F2FF3AD00F7CB7A /* EHSelectionViewController.swift */,
FD96E4211F30F3450008DEE0 /* EHPinningOptions.swift */,
);
path = LearnAboutStackViews;
sourceTree = "<group>";
Expand Down Expand Up @@ -149,6 +156,8 @@
FD07155E1F300A6300F7CB7A /* UILayoutConstraintAxis+Extensions.swift */,
FD0715601F300F4100F7CB7A /* UIStackViewDistribution+Extensions.swift */,
FD0715621F3011F100F7CB7A /* UIStackViewAlignment+Extensions.swift */,
FD0715641F3027D400F7CB7A /* CGFloat+Extensions.swift */,
FD96E41F1F30DF810008DEE0 /* Bool+Extensions.swift */,
);
name = Extensions;
sourceTree = "<group>";
Expand Down Expand Up @@ -237,7 +246,9 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
FD0715651F3027D400F7CB7A /* CGFloat+Extensions.swift in Sources */,
FDD291431F2E18C2009CC565 /* ViewController.swift in Sources */,
FD96E4221F30F3450008DEE0 /* EHPinningOptions.swift in Sources */,
FDD291701F2E3656009CC565 /* EHStackViewSettingsViewController.swift in Sources */,
FDD291411F2E18C2009CC565 /* AppDelegate.swift in Sources */,
FD07155D1F2FF3AD00F7CB7A /* EHSelectionViewController.swift in Sources */,
Expand All @@ -247,6 +258,7 @@
FD0715611F300F4100F7CB7A /* UIStackViewDistribution+Extensions.swift in Sources */,
FD07155F1F300A6300F7CB7A /* UILayoutConstraintAxis+Extensions.swift in Sources */,
FDD291751F2E7060009CC565 /* EHStackViewSettingsModel.swift in Sources */,
FD96E4201F30DF810008DEE0 /* Bool+Extensions.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
Binary file not shown.
46 changes: 46 additions & 0 deletions LearnAboutStackViews/LearnAboutStackViews/Bool+Extensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// Bool+Extensions.swift
// LearnAboutStackViews
//
// Created by Eric Hyche on 8/1/17.
// Copyright © 2017 HeirPlay Software. All rights reserved.
//

import Foundation

extension Bool: EHMultipleChoiceDataSource {

func title() -> String {
return "Boolean Selection"
}

func choiceCount() -> Int {
return 2
}

func choiceText(atIndex: Int) -> String {
let indexChoice: Bool = (atIndex == 0 ? true : false)
return "\(indexChoice)"
}

func isChoiceSelected(atIndex: Int) -> Bool {
let indexChoice: Bool = (atIndex == 0 ? true : false)
return self == indexChoice
}

func canSelectMultipleChoices() -> Bool {
return false
}

func canSelectChoice(atIndex: Int) -> Bool {
return true
}

mutating func setChoice(selected: Bool, atIndex: Int) {
if atIndex < choiceCount() && selected {
let indexChoice: Bool = (atIndex == 0 ? true : false)
self = indexChoice
}
}

}
48 changes: 48 additions & 0 deletions LearnAboutStackViews/LearnAboutStackViews/CGFloat+Extensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//
// CGFloat+Extensions.swift
// LearnAboutStackViews
//
// Created by Eric Hyche on 7/31/17.
// Copyright © 2017 HeirPlay Software. All rights reserved.
//

import UIKit

extension CGFloat: EHMultipleChoiceDataSource {

static let numberOfChoices = 25
static let choiceDelta: CGFloat = 5.0

func title() -> String {
return "CGFloat Selection"
}

func choiceCount() -> Int {
return CGFloat.numberOfChoices
}

func choiceText(atIndex: Int) -> String {
return "\(CGFloat(atIndex) * CGFloat.choiceDelta)"
}

func isChoiceSelected(atIndex: Int) -> Bool {
let indexValue = CGFloat(atIndex) * CGFloat.choiceDelta
return indexValue == self
}

func canSelectMultipleChoices() -> Bool {
return false
}

func canSelectChoice(atIndex: Int) -> Bool {
return true
}

mutating func setChoice(selected: Bool, atIndex: Int) {
if atIndex < choiceCount() && selected {
let indexValue = CGFloat(atIndex) * CGFloat.choiceDelta
self = indexValue
}
}

}
114 changes: 114 additions & 0 deletions LearnAboutStackViews/LearnAboutStackViews/EHPinningOptions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
//
// EHPinningOptions.swift
// LearnAboutStackViews
//
// Created by Eric Hyche on 8/1/17.
// Copyright © 2017 HeirPlay Software. All rights reserved.
//

import UIKit

struct EHPinningOptions: OptionSet {
let rawValue: Int

static let none = EHPinningOptions(rawValue: 0)
static let leading = EHPinningOptions(rawValue: 1 << 0)
static let centerX = EHPinningOptions(rawValue: 1 << 1)
static let trailing = EHPinningOptions(rawValue: 1 << 2)
static let top = EHPinningOptions(rawValue: 1 << 3)
static let centerY = EHPinningOptions(rawValue: 1 << 4)
static let bottom = EHPinningOptions(rawValue: 1 << 5)

var textDescription: String {
get {
return EHPinningOptions.toString(options: self)
}
}

static func toString(options: EHPinningOptions) -> String {
var optionStrings = [String]()

if options.contains(.leading) {
optionStrings.append("leading")
}
if options.contains(.centerX) {
optionStrings.append("centerX")
}
if options.contains(.trailing) {
optionStrings.append("trailing")
}
if options.contains(.top) {
optionStrings.append("top")
}
if options.contains(.centerY) {
optionStrings.append("centerY")
}
if options.contains(.bottom) {
optionStrings.append("bottom")
}

var desc = ""
if optionStrings.isEmpty {
desc = "none"
} else if optionStrings.count == 1 {
desc = optionStrings[0]
} else {
desc = "\(optionStrings)"
}

return desc
}

}

extension EHPinningOptions: EHMultipleChoiceDataSource {

func title() -> String {
return "Pinning Options"
}

func choiceCount() -> Int {
return 6
}

func choiceText(atIndex: Int) -> String {
var text = "unknown"

if atIndex < choiceCount() {
let option = EHPinningOptions(rawValue: (1 << atIndex))
text = option.textDescription
}

return text
}

func isChoiceSelected(atIndex: Int) -> Bool {
var selected = false

if atIndex < choiceCount() {
selected = contains(EHPinningOptions(rawValue: (1 << atIndex)))
}

return selected
}

func canSelectMultipleChoices() -> Bool {
return true
}

func canSelectChoice(atIndex: Int) -> Bool {
return true
}

mutating func setChoice(selected: Bool, atIndex: Int) {
if atIndex < choiceCount() {
let option = EHPinningOptions(rawValue: (1 << atIndex))
if selected {
insert(option)
} else {
remove(option)
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,6 @@ import UIKit
typealias EHStackViewSettingsAnimationBlock = () -> Void
typealias EHStackViewSettingsCompletionBlock = (_ finished: Bool) -> Void

struct EHPinningOptions: OptionSet {
let rawValue: Int

static let none = EHPinningOptions(rawValue: 0)
static let top = EHPinningOptions(rawValue: 1 << 0)
static let centerY = EHPinningOptions(rawValue: 1 << 1)
static let bottom = EHPinningOptions(rawValue: 1 << 2)
static let leading = EHPinningOptions(rawValue: 1 << 3)
static let centerX = EHPinningOptions(rawValue: 1 << 4)
static let trailing = EHPinningOptions(rawValue: 1 << 5)
}

struct EHStackViewSettingsModel {
var axis: UILayoutConstraintAxis
var distribution: UIStackViewDistribution
Expand Down Expand Up @@ -114,8 +102,9 @@ extension EHStackViewSettingsModel {
static func computePinning(forStackView stackView: UIStackView, inContainerView containerView: UIView) -> EHPinningOptions {
// Get all the contraints on the stackView
var pinning: EHPinningOptions = EHPinningOptions.none
let constraints = stackView.constraints
let constraints = containerView.constraints
for constraint in constraints {
print("constraint=\(constraint)")
// This has to be a constraint between two views
if let firstView = constraint.firstItem as? UIView,
let secondView = constraint.secondItem as? UIView {
Expand All @@ -129,8 +118,10 @@ extension EHStackViewSettingsModel {
stackViewAttribute = constraint.secondAttribute
containerViewAttribute = constraint.firstAttribute
}
let pinOption = pinningOption(stackViewAttribute: stackViewAttribute, containerViewAttribute: containerViewAttribute)
pinning.update(with: pinOption)
if stackViewAttribute != .notAnAttribute && containerViewAttribute != .notAnAttribute {
let pinOption = pinningOption(stackViewAttribute: stackViewAttribute, containerViewAttribute: containerViewAttribute)
pinning.insert(pinOption)
}
}
}
return pinning
Expand Down
Loading

0 comments on commit 1d51266

Please sign in to comment.