Created
October 8, 2017 20:03
-
-
Save ApolloZhu/38818dc55d6a6970f41bc59c2b02d6e8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.swift | |
// layout | |
// | |
// Created by Liuliet.Lee on 10/8/2017. | |
// Copyright © 2017 Liuliet.Lee. All rights reserved. | |
// | |
import UIKit | |
class ViewController: UIViewController { | |
// MARK: Subviews | |
private lazy var viewDict: [String: UIView] = [ | |
"fadeButton": self.button(bColor: .darkGray, title: "fade", selector: #selector(fade)), | |
"gap": UIView(), | |
"moveButton": self.button(bColor: .orange, title: "move", selector: #selector(move)) | |
] | |
private var fadeButton: UIView! { return viewDict["fadeButton"] } | |
private var gap: UIView! { return viewDict["gap"] } | |
private var moveButton: UIView! { return viewDict["moveButton"] } | |
// MARK: Constraints | |
private lazy var cons: [NSLayoutConstraint] = [ | |
self.fadeButton.widthAnchor.constraint(equalToConstant: 200.0), | |
self.fadeButton.heightAnchor.constraint(equalToConstant: 100.0), | |
self.moveButton.widthAnchor.constraint(equalTo: self.fadeButton.widthAnchor), | |
self.moveButton.heightAnchor.constraint(equalTo: self.fadeButton.heightAnchor), | |
self.gap.centerXAnchor.constraint(equalTo: self.view.centerXAnchor), | |
self.gap.centerYAnchor.constraint(equalTo: self.view.centerYAnchor) | |
] | |
private var currentAdditionalCons = [NSLayoutConstraint]() | |
private lazy var vCons: [NSLayoutConstraint] = NSLayoutConstraint.constraints(withVisualFormat: "V:[fadeButton]-[gap(==160)]-[moveButton]", options: [.alignAllCenterX], metrics: nil, views: self.viewDict) | |
private lazy var hCons: [NSLayoutConstraint] = NSLayoutConstraint.constraints(withVisualFormat: "H:[fadeButton]-[gap(==300)]-[moveButton]", options: [.alignAllCenterY], metrics: nil, views: self.viewDict) | |
@objc private func configureConstraints() { | |
NSLayoutConstraint.deactivate(currentAdditionalCons) | |
defer { NSLayoutConstraint.activate(currentAdditionalCons) } | |
if traitCollection.horizontalSizeClass == .compact | |
|| !UIDevice.current.orientation.isLandscape { | |
currentAdditionalCons = vCons | |
} else { | |
currentAdditionalCons = hCons | |
} | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
for subview in viewDict.values { | |
// Important: Must disable autoresizing mask | |
subview.translatesAutoresizingMaskIntoConstraints = false | |
view.addSubview(subview) | |
} | |
NSLayoutConstraint.activate(cons) | |
// MARK: Update Observers | |
NotificationCenter.default.addObserver(self, selector: #selector(configureConstraints), name: .UIDeviceOrientationDidChange, object: nil) | |
} | |
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) { | |
super.traitCollectionDidChange(previousTraitCollection) | |
configureConstraints() | |
} | |
// MARK: Auxiliary | |
private func button(bColor: UIColor, title: String, selector: Selector) -> UIButton { | |
let button = UIButton() | |
button.translatesAutoresizingMaskIntoConstraints = false | |
button.backgroundColor = bColor | |
button.setTitleColor(.white, for: .normal) | |
button.setTitle(title, for: .normal) | |
button.titleLabel?.font = UIFont(name: "Avenir", size: 36.0) | |
button.layer.cornerRadius = 16.0 | |
button.addTarget(self, action: selector, for: .touchUpInside) | |
return button | |
} | |
@objc private func fade() { | |
UIView.animate(withDuration: 0.6, delay: 0.0, options: [.autoreverse], | |
animations: { self.fadeButton.alpha = 0.0 }, | |
completion: { isFinished in if isFinished { self.fadeButton.alpha = 1.0 } }) | |
} | |
@objc private func move() { | |
UIView.animate(withDuration: 0.6, delay: 0.0, options: [.autoreverse], | |
animations: { self.moveButton.frame.origin.y += 100.0 }, | |
completion: { isFinished in if isFinished { self.moveButton.frame.origin.y -= 100.0 } }) | |
} | |
} | |
extension UIDeviceOrientation { | |
fileprivate var isLandscape: Bool { | |
return self == .landscapeLeft || self == .landscapeRight | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment