Created
July 17, 2018 17:22
-
-
Save onurgenes/0f1006f6f454f0dbf7b16ea238e030cd to your computer and use it in GitHub Desktop.
SteviaLayout Animation
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 | |
// steviademo | |
// | |
// Created by Onur Geneş on 17.07.2018. | |
// Copyright © 2018 Onur Geneş. All rights reserved. | |
// | |
import UIKit | |
import Stevia | |
class ViewController: UIViewController, UITextFieldDelegate { | |
lazy var topView: UIView = { | |
let v = UIView() | |
v.backgroundColor = .red | |
return v | |
}() | |
lazy var bottomView: UIView = { | |
let v = UIView() | |
v.backgroundColor = .blue | |
return v | |
}() | |
lazy var collectionView: UIView = { | |
let v = UIView() | |
v.backgroundColor = .green | |
return v | |
}() | |
lazy var addButtonView: UIView = { | |
let v = UIView() | |
v.backgroundColor = .yellow | |
v.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleKeyboard))) | |
v.isUserInteractionEnabled = true | |
return v | |
}() | |
@objc func handleKeyboard() { | |
self.view.layout( | |
|-0-self.bottomView.height(50)-0-|, | |
150, | |
|-0-self.collectionView.height(50)-0-|, | |
0 | |
) | |
self.view.setNeedsLayout() | |
UIView.animate(withDuration: 0.5) { | |
self.view.layoutIfNeeded() | |
} | |
} | |
lazy var sendButtonView: UIView = { | |
let v = UIView() | |
v.backgroundColor = .yellow | |
v.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleKeyboardDismiss))) | |
v.isUserInteractionEnabled = true | |
return v | |
}() | |
@objc func handleKeyboardDismiss() { | |
view.setNeedsLayout() | |
self.view.layout( | |
|-0-self.bottomView.height(50)-0-|, | |
0, | |
|-0-self.collectionView.height(50)-0-|, | |
0 | |
) | |
UIView.animate(withDuration: 0.5) { | |
self.view.layoutIfNeeded() | |
} | |
} | |
lazy var textField: UITextField = { | |
let tf = UITextField() | |
tf.borderStyle = .roundedRect | |
tf.layer.cornerRadius = 20 | |
tf.layer.masksToBounds = true | |
tf.returnKeyType = .done | |
return tf | |
}() | |
lazy var backgroundView: UIView = { | |
let v = UIView() | |
v.backgroundColor = .brown | |
return v | |
}() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
view.backgroundColor = .white | |
textField.delegate = self | |
view.sv( | |
backgroundView, | |
topView, | |
bottomView.sv( | |
addButtonView, | |
textField, | |
sendButtonView | |
), | |
collectionView | |
) | |
view.layout( | |
0, | |
|-0-backgroundView-0-|, | |
0 | |
) | |
view.layout( | |
0, | |
|-0-topView.height(100)-0-| | |
) | |
view.layout( | |
|-0-bottomView.height(50)-0-|, | |
0, | |
|-0-collectionView.height(50)-0-|, | |
0 | |
) | |
bottomView.layout( | |
5, | |
|-5-addButtonView.width(40)-5-textField-5-sendButtonView.width(40).heightEqualsWidth()-5-|, | |
5 | |
) | |
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShowAnimation), name: NSNotification.Name.UIKeyboardWillShow, object: nil) | |
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHideAnimation), name: NSNotification.Name.UIKeyboardWillHide, object: nil) | |
} | |
@objc func keyboardWillShowAnimation(notification: Notification) { | |
guard let keyboardRect = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else { return } | |
if notification.name == NSNotification.Name.UIKeyboardWillShow { | |
self.view.setNeedsLayout() | |
self.view.layout( | |
|-0-self.bottomView.height(50)-0-|, | |
keyboardRect.height, | |
|-0-self.collectionView.height(50)-0-|, | |
0 | |
) | |
UIView.animate(withDuration: 0.5) { | |
self.view.layoutIfNeeded() | |
} | |
} | |
} | |
@objc func keyboardWillHideAnimation(notification: Notification) { | |
//guard let keyboardRect = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else { return } | |
if notification.name == NSNotification.Name.UIKeyboardWillHide { | |
UIView.animate(withDuration: 0.5) { | |
self.view.layout( | |
|-0-self.bottomView.height(50)-0-|, | |
0, | |
|-0-self.collectionView.height(50)-0-|, | |
0 | |
) | |
self.view.layoutIfNeeded() | |
} | |
} | |
} | |
func textFieldShouldReturn(_ textField: UITextField) -> Bool { | |
view.endEditing(true) | |
return true | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment