-
Notifications
You must be signed in to change notification settings - Fork 1
/
Contents.swift
65 lines (48 loc) · 2.33 KB
/
Contents.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import UIKit
import PlaygroundSupport
import RxSwift
import RxCocoa
//: 2.6 merge プリミティブ:ストリームをマージする
class ViewController : UIViewController {
let disposeBag = DisposeBag()
override func loadView() {
// Create views
let view = UIView()
view.backgroundColor = .white
self.view = view
let onegai = UIButton(type: .system)
onegai.setTitle("Onegai shimasu", for: .normal)
view.addSubview(onegai)
let thanks = UIButton(type: .system)
thanks.setTitle("Thank you", for: .normal)
view.addSubview(thanks)
let textField = UITextField()
textField.borderStyle = .roundedRect
textField.textAlignment = .center
view.addSubview(textField)
// Layout views
let margins = view.layoutMarginsGuide
textField.translatesAutoresizingMaskIntoConstraints = false
textField.leadingAnchor.constraint(equalTo: margins.leadingAnchor).isActive = true
textField.trailingAnchor.constraint(equalTo: margins.trailingAnchor).isActive = true
textField.centerYAnchor.constraint(equalTo: margins.centerYAnchor).isActive = true
thanks.translatesAutoresizingMaskIntoConstraints = false
thanks.bottomAnchor.constraint(equalTo: textField.topAnchor).isActive = true
thanks.leadingAnchor.constraint(equalTo: margins.leadingAnchor).isActive = true
thanks.trailingAnchor.constraint(equalTo: margins.trailingAnchor).isActive = true
thanks.heightAnchor.constraint(equalToConstant: 20).isActive = true
onegai.translatesAutoresizingMaskIntoConstraints = false
onegai.bottomAnchor.constraint(equalTo: thanks.topAnchor).isActive = true
onegai.leadingAnchor.constraint(equalTo: margins.leadingAnchor).isActive = true
onegai.trailingAnchor.constraint(equalTo: margins.trailingAnchor).isActive = true
onegai.heightAnchor.constraint(equalToConstant: 20).isActive = true
// Merge streams
let sOnegai = onegai.rx.tap.map { _ in "Onegai shimasu" }
let sThanks = thanks.rx.tap.map { _ in "Thank you" }
let sCanned = Observable.of(sOnegai, sThanks).merge()
sCanned
.bind(to: textField.rx.text)
.disposed(by: disposeBag)
}
}
PlaygroundPage.current.liveView = ViewController()