Skip to content

Commit

Permalink
Updates to sample apps
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric Hyche committed Aug 5, 2017
1 parent b80ee74 commit 480d23b
Show file tree
Hide file tree
Showing 41 changed files with 1,694 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import UIKit
import PlaygroundSupport

let liveView = EHAdjustableContainerView(frame: CGRect(x: 0.0, y: 0.0, width: 512.0, height: 512.0))

let label0 = UILabel(frame: .zero)
label0.translatesAutoresizingMaskIntoConstraints = false
label0.backgroundColor = .green
label0.text = "111"
label0.textColor = .white
label0.font = UIFont.systemFont(ofSize: 18.0)

liveView.containerView.addSubview(label0)

let label1 = UILabel(frame: .zero)
label1.translatesAutoresizingMaskIntoConstraints = false
label1.backgroundColor = .red
label1.text = "22222"
label1.textColor = .white
label1.font = UIFont.systemFont(ofSize: 24.0)

liveView.containerView.addSubview(label1)

label0.centerYAnchor.constraint(equalTo: liveView.containerView.centerYAnchor).isActive = true
label1.centerYAnchor.constraint(equalTo: liveView.containerView.centerYAnchor).isActive = true

let horzConstraints = NSLayoutConstraint.constraints(withVisualFormat: "H:|-(20)-[label0]-(20)-[label1]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["label0": label0, "label1": label1])
NSLayoutConstraint.activate(horzConstraints)


PlaygroundPage.current.needsIndefiniteExecution = true
PlaygroundPage.current.liveView = liveView

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Timeline
version = "3.0">
<TimelineItems>
</TimelineItems>
</Timeline>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//: [Previous](@previous)

import Foundation

var str = "Hello, playground"

//: [Next](@next)
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import UIKit

public class EHAdjustableContainerView : UIView {

// MARK: - Public properties

public let containerView = UIView(frame: .zero)

// MARK: - Private properties

private let internalContainerView = UIView(frame: .zero)
private let containerSizeInfoView = EHContainerSizeInfoView(frame: .zero)
private var containerViewWidthConstraint = NSLayoutConstraint()
private var containerViewHeightConstraint = NSLayoutConstraint()

// MARK: - Initializers

override public init(frame: CGRect) {
super.init(frame: frame)
setupView()
}

required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

// MARK: - UIView methods

override public func layoutSubviews() {
super.layoutSubviews()

containerSizeInfoView.maxContainerViewSize = internalContainerView.frame.size
}


// MARK: - Private methods

func setupView() {
backgroundColor = UIColor.white

internalContainerView.translatesAutoresizingMaskIntoConstraints = false
internalContainerView.backgroundColor = UIColor.white
internalContainerView.layer.borderColor = UIColor.black.cgColor
internalContainerView.layer.borderWidth = 2.0
addSubview(internalContainerView)

containerView.translatesAutoresizingMaskIntoConstraints = false
containerView.backgroundColor = UIColor.lightGray
internalContainerView.addSubview(containerView)

containerView.centerXAnchor.constraint(equalTo: internalContainerView.centerXAnchor).isActive = true
containerView.centerYAnchor.constraint(equalTo: internalContainerView.centerYAnchor).isActive = true

containerViewWidthConstraint = containerView.widthAnchor.constraint(equalToConstant: 256.0)
containerViewHeightConstraint = containerView.heightAnchor.constraint(equalToConstant: 256.0)
containerViewWidthConstraint.isActive = true
containerViewHeightConstraint.isActive = true

containerSizeInfoView.translatesAutoresizingMaskIntoConstraints = false
containerSizeInfoView.backgroundColor = UIColor.white
addSubview(containerSizeInfoView)

internalContainerView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 10.0).isActive = true
trailingAnchor.constraint(equalTo: internalContainerView.trailingAnchor, constant: 10.0).isActive = true
internalContainerView.topAnchor.constraint(equalTo: topAnchor, constant: 10.0).isActive = true

containerSizeInfoView.topAnchor.constraint(equalTo: internalContainerView.bottomAnchor, constant: 10.0).isActive = true

containerSizeInfoView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 10.0).isActive = true
trailingAnchor.constraint(equalTo: containerSizeInfoView.trailingAnchor, constant: 10.0).isActive = true
bottomAnchor.constraint(equalTo: containerSizeInfoView.bottomAnchor, constant: 10.0).isActive = true

containerSizeInfoView.containerViewSizeChanged = { [weak self] (size) in
self?.containerViewWidthConstraint.constant = size.width
self?.containerViewHeightConstraint.constant = size.height
}
}

}

143 changes: 143 additions & 0 deletions LearnAboutAutoLayout.playground/Sources/EHContainerSizeInfoView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import UIKit

public typealias EHContainerViewSizeChangedBlock = (CGSize) -> Void

public class EHContainerSizeInfoView: UIView {

// MARK: - Public properties
public let minContainerViewSize = CGSize(width: 40.0, height: 40.0)
public var maxContainerViewSize = CGSize(width: 256.0, height: 256.0) {
didSet {
if maxContainerViewSize != oldValue {
updateWidth()
updateHeight()
}
}
}
public var containerViewSize = CGSize(width: 256.0, height: 128.0) {
didSet {
if containerViewSize != oldValue {
containerViewSizeChanged?(containerViewSize)
}
}
}
public var containerViewSizeChanged: EHContainerViewSizeChangedBlock?

// MARK: - Private properties
private let widthSlider = UISlider(frame: .zero)
private let heightSlider = UISlider(frame: .zero)
private let widthLabel = UILabel(frame: .zero)
private let heightLabel = UILabel(frame: .zero)
private let verticalStackView = UIStackView(frame: .zero)
private let widthStackView = UIStackView(frame: .zero)
private let heightStackView = UIStackView(frame: .zero)

// MARK: - Initializers
override public init(frame: CGRect) {
super.init(frame: frame)
setupViews()
}

required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

// MARK: - Private methods
private func setupViews() {

// Set up the width horizontal stack view
widthStackView.translatesAutoresizingMaskIntoConstraints = false
widthStackView.axis = .horizontal
widthStackView.distribution = .fill
widthStackView.alignment = .center
widthStackView.spacing = 10.0

widthSlider.translatesAutoresizingMaskIntoConstraints = false
widthSlider.minimumValue = 0.0
widthSlider.maximumValue = 1.0
widthSlider.value = 1.0
widthSlider.isContinuous = true
widthSlider.addTarget(self, action: #selector(widthSliderChanged(slider:)), for: .valueChanged)

widthStackView.addArrangedSubview(widthSlider)

widthLabel.translatesAutoresizingMaskIntoConstraints = false
widthLabel.font = UIFont.systemFont(ofSize: 16.0)
widthLabel.textAlignment = .right
widthLabel.textColor = UIColor.black
widthLabel.text = "\(Int(computedWidth()))"

widthStackView.addArrangedSubview(widthLabel)

// Set up the height horizontal stack view
heightStackView.translatesAutoresizingMaskIntoConstraints = false
heightStackView.axis = .horizontal
heightStackView.distribution = .fill
heightStackView.alignment = .center
heightStackView.spacing = 10.0

heightSlider.translatesAutoresizingMaskIntoConstraints = false
heightSlider.minimumValue = 0.0
heightSlider.maximumValue = 1.0
heightSlider.value = 1.0
heightSlider.isContinuous = true
heightSlider.addTarget(self, action: #selector(heightSliderChanged(slider:)), for: .valueChanged)

heightStackView.addArrangedSubview(heightSlider)

heightLabel.translatesAutoresizingMaskIntoConstraints = false
heightLabel.font = UIFont.systemFont(ofSize: 16.0)
heightLabel.textAlignment = .right
heightLabel.textColor = UIColor.black
heightLabel.text = "\(Int(computedHeight()))"

heightStackView.addArrangedSubview(heightLabel)

// Now set up the vertical stack view

verticalStackView.translatesAutoresizingMaskIntoConstraints = false
verticalStackView.axis = .vertical
verticalStackView.distribution = .fill
verticalStackView.alignment = .fill
verticalStackView.spacing = 10.0

verticalStackView.addArrangedSubview(widthStackView)
verticalStackView.addArrangedSubview(heightStackView)

addSubview(verticalStackView)

verticalStackView.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
verticalStackView.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
verticalStackView.topAnchor.constraint(equalTo: topAnchor).isActive = true
verticalStackView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true

}

@objc private func widthSliderChanged(slider: UISlider) {
updateWidth()
}

@objc private func heightSliderChanged(slider: UISlider) {
updateHeight()
}

private func computedWidth() -> CGFloat {
return (maxContainerViewSize.width - minContainerViewSize.width) * CGFloat(widthSlider.value) + minContainerViewSize.width
}

private func computedHeight() -> CGFloat {
return (maxContainerViewSize.height - minContainerViewSize.height) * CGFloat(heightSlider.value) + minContainerViewSize.height
}

private func updateWidth() {
let width = computedWidth()
widthLabel.text = "\(Int(width))"
containerViewSize = CGSize(width: width, height: containerViewSize.height)
}

private func updateHeight() {
let height = computedHeight()
heightLabel.text = "\(Int(height))"
containerViewSize = CGSize(width: containerViewSize.width, height: height)
}
}
7 changes: 7 additions & 0 deletions LearnAboutAutoLayout.playground/contents.xcplayground
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='6.0' target-platform='ios'>
<pages>
<page name='Introducing Constraints'/>
<page name='Untitled Page 2'/>
</pages>
</playground>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
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 */; };
FD2C0CFA1F35574A00C41E63 /* EHStackViewContentViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = FD2C0CF91F35574A00C41E63 /* EHStackViewContentViewController.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 */; };
Expand Down Expand Up @@ -42,6 +43,7 @@
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>"; };
FD2C0CF91F35574A00C41E63 /* EHStackViewContentViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EHStackViewContentViewController.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; };
Expand Down Expand Up @@ -111,6 +113,7 @@
FDD291741F2E7060009CC565 /* EHStackViewSettingsModel.swift */,
FD07155C1F2FF3AD00F7CB7A /* EHSelectionViewController.swift */,
FD96E4211F30F3450008DEE0 /* EHPinningOptions.swift */,
FD2C0CF91F35574A00C41E63 /* EHStackViewContentViewController.swift */,
);
path = LearnAboutStackViews;
sourceTree = "<group>";
Expand Down Expand Up @@ -253,6 +256,7 @@
FDD291411F2E18C2009CC565 /* AppDelegate.swift in Sources */,
FD07155D1F2FF3AD00F7CB7A /* EHSelectionViewController.swift in Sources */,
FD0715631F3011F100F7CB7A /* UIStackViewAlignment+Extensions.swift in Sources */,
FD2C0CFA1F35574A00C41E63 /* EHStackViewContentViewController.swift in Sources */,
FDD291531F2E19EE009CC565 /* EHColorableStackView.swift in Sources */,
FDD2916E1F2E2B69009CC565 /* UIColor+Extensions.swift in Sources */,
FD0715611F300F4100F7CB7A /* UIStackViewDistribution+Extensions.swift in Sources */,
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "trash.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "[email protected]",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "[email protected]",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ class EHMultipleChoiceViewController: UIViewController {
self.dataSource = dataSource
super.init(nibName: nil, bundle: nil)
navigationItem.title = dataSource.title()
navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(updateButtonTapped(sender:)))
}

required init?(coder aDecoder: NSCoder) {
Expand Down Expand Up @@ -138,20 +137,20 @@ class EHMultipleChoiceViewController: UIViewController {
dataSource.setChoice(selected: !isRowSelected, atIndex: rowTappedIndex)
// Now update the entire view state
updateViewState()
// Call the datasource updated block
updateBlock?(dataSource)
} else {
// This is a single-select data source, so we only take action
// if the row is not selected.
if !isRowSelected {
dataSource.setChoice(selected: true, atIndex: rowTappedIndex)
// Now update the entire view state
updateViewState()
// Call the datasource updated block
updateBlock?(dataSource)
}
}
}
}

@objc func updateButtonTapped(sender: AnyObject) {
updateBlock?(dataSource)
}

}
Loading

0 comments on commit 480d23b

Please sign in to comment.