|
| 1 | +// |
| 2 | +// ViewController.swift |
| 3 | +// StackScrollView |
| 4 | +// |
| 5 | +// Created by muukii on 08/29/2016. |
| 6 | +// Copyright (c) 2016 muukii. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import UIKit |
| 10 | + |
| 11 | +import StackScrollView |
| 12 | +import EasyPeasy |
| 13 | + |
| 14 | +import Then |
| 15 | + |
| 16 | +class ViewController: UIViewController { |
| 17 | + |
| 18 | + override func viewDidLoad() { |
| 19 | + super.viewDidLoad() |
| 20 | + |
| 21 | + let colorA = UIColor(white: 0.9, alpha: 1) |
| 22 | + let colorB = UIColor(white: 0.8, alpha: 1) |
| 23 | + |
| 24 | + |
| 25 | + let labelCell = LabelStackViewCell().then { cell in |
| 26 | + cell.backgroundColor = colorA |
| 27 | + cell.titleLabel.text = "Hiroshi" |
| 28 | + cell.detailLabel.text = "Kimura" |
| 29 | + } |
| 30 | + |
| 31 | + let switchCell = SwitchStackViewCell().then { cell in |
| 32 | + cell.backgroundColor = colorB |
| 33 | + cell.titleLabel.text = "Hiroshi" |
| 34 | + cell.valueChanged = { on in |
| 35 | + |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + let labelFromTextCell = LabelStackViewCell().then { cell in |
| 40 | + cell.backgroundColor = colorA |
| 41 | + cell.titleLabel.text = "Hiroshi" |
| 42 | + cell.detailLabel.text = "Kimura" |
| 43 | + } |
| 44 | + |
| 45 | + let textFieldCell = TextFieldStackViewCell().then { cell in |
| 46 | + cell.backgroundColor = colorB |
| 47 | + cell.titleLabel.text = "Hiroshi" |
| 48 | + cell.valueChanged = { string in |
| 49 | + |
| 50 | + labelFromTextCell.titleLabel.text = string |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + stackScrollView.append( |
| 55 | + views: [ |
| 56 | + labelCell, |
| 57 | + switchCell, |
| 58 | + labelFromTextCell, |
| 59 | + textFieldCell, |
| 60 | + ], |
| 61 | + animated: true) |
| 62 | + } |
| 63 | + |
| 64 | + override func didReceiveMemoryWarning() { |
| 65 | + super.didReceiveMemoryWarning() |
| 66 | + // Dispose of any resources that can be recreated. |
| 67 | + } |
| 68 | + |
| 69 | + @IBOutlet weak var stackScrollView: StackScrollView! |
| 70 | +} |
| 71 | + |
| 72 | +class LabelStackViewCell: UIView { |
| 73 | + |
| 74 | + init() { |
| 75 | + super.init(frame: .zero) |
| 76 | + |
| 77 | + addSubview(titleLabel) |
| 78 | + addSubview(detailLabel) |
| 79 | + |
| 80 | + titleLabel <- [ |
| 81 | + Left(8), |
| 82 | + CenterY(), |
| 83 | + ] |
| 84 | + |
| 85 | + detailLabel <- [ |
| 86 | + Right(8), |
| 87 | + CenterY(), |
| 88 | + ] |
| 89 | + } |
| 90 | + |
| 91 | + override func intrinsicContentSize() -> CGSize { |
| 92 | + return CGSize(width: UIViewNoIntrinsicMetric, height: 60) |
| 93 | + } |
| 94 | + |
| 95 | + required init?(coder aDecoder: NSCoder) { |
| 96 | + fatalError("init(coder:) has not been implemented") |
| 97 | + } |
| 98 | + |
| 99 | + let titleLabel = UILabel() |
| 100 | + let detailLabel = UILabel() |
| 101 | +} |
| 102 | + |
| 103 | +class SwitchStackViewCell: UIView { |
| 104 | + |
| 105 | + var valueChanged: (Bool) -> Void = { _ in } |
| 106 | + |
| 107 | + init() { |
| 108 | + super.init(frame: .zero) |
| 109 | + |
| 110 | + addSubview(titleLabel) |
| 111 | + addSubview(switchView) |
| 112 | + |
| 113 | + titleLabel <- [ |
| 114 | + Left(8), |
| 115 | + CenterY(), |
| 116 | + ] |
| 117 | + |
| 118 | + switchView <- [ |
| 119 | + Right(8), |
| 120 | + CenterY(), |
| 121 | + ] |
| 122 | + |
| 123 | + switchView.addTarget(self, action: #selector(switchValueChanged), forControlEvents: .ValueChanged) |
| 124 | + } |
| 125 | + |
| 126 | + override func intrinsicContentSize() -> CGSize { |
| 127 | + return CGSize(width: UIViewNoIntrinsicMetric, height: 60) |
| 128 | + } |
| 129 | + |
| 130 | + required init?(coder aDecoder: NSCoder) { |
| 131 | + fatalError("init(coder:) has not been implemented") |
| 132 | + } |
| 133 | + |
| 134 | + let titleLabel = UILabel() |
| 135 | + |
| 136 | + let switchView = UISwitch() |
| 137 | + |
| 138 | + @objc private func switchValueChanged() { |
| 139 | + |
| 140 | + valueChanged(switchView.on) |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +class TextFieldStackViewCell: UIView { |
| 145 | + |
| 146 | + var valueChanged: (String) -> Void = { _ in } |
| 147 | + |
| 148 | + init() { |
| 149 | + super.init(frame: .zero) |
| 150 | + |
| 151 | + addSubview(titleLabel) |
| 152 | + addSubview(textField) |
| 153 | + |
| 154 | + titleLabel.setContentHuggingPriority(950, forAxis: .Horizontal) |
| 155 | + titleLabel <- [ |
| 156 | + Left(8), |
| 157 | + CenterY(), |
| 158 | + ] |
| 159 | + |
| 160 | + textField <- [ |
| 161 | + Left(8).to(titleLabel, .Right), |
| 162 | + Right(8), |
| 163 | + CenterY(), |
| 164 | + ] |
| 165 | + |
| 166 | + textField.backgroundColor = UIColor(white: 0, alpha: 0.5) |
| 167 | + |
| 168 | + textField.addTarget(self, action: #selector(textChanged), forControlEvents: .EditingChanged) |
| 169 | + } |
| 170 | + |
| 171 | + override func intrinsicContentSize() -> CGSize { |
| 172 | + return CGSize(width: UIViewNoIntrinsicMetric, height: 60) |
| 173 | + } |
| 174 | + |
| 175 | + required init?(coder aDecoder: NSCoder) { |
| 176 | + fatalError("init(coder:) has not been implemented") |
| 177 | + } |
| 178 | + |
| 179 | + let titleLabel = UILabel() |
| 180 | + |
| 181 | + let textField = UITextField() |
| 182 | + |
| 183 | + @objc private func textChanged() { |
| 184 | + |
| 185 | + valueChanged(textField.text ?? "") |
| 186 | + } |
| 187 | +} |
0 commit comments