-
Notifications
You must be signed in to change notification settings - Fork 26
/
ViewController.swift
117 lines (102 loc) · 4.27 KB
/
ViewController.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//
// ViewController.swift
// MisterFusionSample
//
// Created by 鈴木大貴 on 2015/11/13.
// Copyright © 2015年 Taiki Suzuki. All rights reserved.
//
import UIKit
import MisterFusion
class ViewController: UIViewController {
fileprivate var greenView: UIView?
fileprivate var greenViewBottomConstraint: NSLayoutConstraint?
fileprivate var whiteView: UIView?
fileprivate var redView: UIView?
fileprivate var whiteViewWidthConstraint: NSLayoutConstraint?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let redView = UIView()
redView.backgroundColor = .red
redView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(redView)
//Ordinary AutoLayout code
let toItem: AnyObject
if #available(iOS 11, *) {
toItem = view.safeAreaLayoutGuide
} else {
toItem = view
}
view.addConstraints([
NSLayoutConstraint(item: redView, attribute: .top, relatedBy: .equal, toItem: toItem, attribute: .top, multiplier: 1, constant: 10),
NSLayoutConstraint(item: redView, attribute: .right, relatedBy: .equal, toItem: toItem, attribute: .right, multiplier: 1, constant: -10),
NSLayoutConstraint(item: redView, attribute: .left, relatedBy: .equal, toItem: toItem, attribute: .left, multiplier: 1, constant: 10),
NSLayoutConstraint(item: redView, attribute: .height, relatedBy: .equal, toItem: toItem, attribute: .height, multiplier: 0.5, constant: -15),
])
self.redView = redView
let yellowView = UIView()
yellowView.backgroundColor = .yellow
yellowView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(yellowView)
//MisterFusion code
view.mf.addConstraints(
yellowView.top |==| redView.bottom |+| 10,
yellowView.right |==| view.safeArea.right |-| 10,
yellowView.left |==| view.safeArea.left |+| 10,
yellowView.height |==| view.safeArea.height |/| 2 |-| 15
)
let greenView = UIView()
greenView.backgroundColor = .green
//Advanced MisterFusion code
greenViewBottomConstraint = yellowView.mf.addSubview(greenView, andConstraints:
greenView.top,
greenView.right,
greenView.bottom,
greenView.width |*| 0.5
).firstAttribute(.bottom).first
self.greenView = greenView
let whiteView = UIView()
whiteView.backgroundColor = .white
redView.mf.addSubview(whiteView, andConstraints:
whiteView.bottom |-| 10,
whiteView.left |+| 10,
whiteView.height |==| 100
)
self.whiteView = whiteView
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
reduceAnimation()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
guard let whiteView = whiteView, let redView = redView else { return }
if let whiteViewHeightConstraint = whiteViewWidthConstraint {
redView.removeConstraint(whiteViewHeightConstraint)
}
self.whiteViewWidthConstraint = redView.mf.addConstraints(
whiteView.width |-| 20 <|> .compact,
whiteView.width |*| 0.5 |-| 10 <|> .regular <-> .compact
).firstAttribute(.width).first
}
func stretchAnmation() {
greenViewBottomConstraint?.constant = 0
UIView.animate(withDuration: 2, animations: {
self.greenView?.superview?.layoutIfNeeded()
}) { _ in
self.reduceAnimation()
}
}
func reduceAnimation() {
let constant = self.greenView?.superview?.frame.size.height ?? 0
greenViewBottomConstraint?.constant = -constant / 2
UIView.animate(withDuration: 2, animations: {
self.greenView?.superview?.layoutIfNeeded()
}) { _ in
self.stretchAnmation()
}
}
}