Skip to content

Commit

Permalink
Animate card to make room for keyboard
Browse files Browse the repository at this point in the history
  • Loading branch information
fakerabbit committed Mar 5, 2017
1 parent 07c477b commit fdab58d
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 8 deletions.
13 changes: 7 additions & 6 deletions Memoria/CreateCard.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class CreateCard: UIView {
lbl.textColor = Utils.backgroundColor()
lbl.text = "Category"
lbl.sizeToFit()
lbl.frame.size.height += 5
return lbl
}()

Expand Down Expand Up @@ -106,15 +107,15 @@ class CreateCard: UIView {
super.layoutSubviews()
let w = self.frame.size.width
self.layer.shadowRadius = w/5.5
let padY:CGFloat = 10.0
let padY:CGFloat = 40.0
qLbl.frame = CGRect(x: pad, y: pad, width: w - pad * 2, height: qLbl.frame.size.height)
qTfld.frame = CGRect(x: qLbl.frame.minX, y: qLbl.frame.maxY - padY, width: qLbl.frame.size.width, height: textFieldH)
qTfld.frame = CGRect(x: qLbl.frame.minX, y: qLbl.frame.maxY, width: qLbl.frame.size.width, height: textFieldH)
aLbl.frame = CGRect(x: qLbl.frame.minX, y: qTfld.frame.maxY + padY, width: qLbl.frame.size.width, height: aLbl.frame.size.height)
aTfld.frame = CGRect(x: qLbl.frame.minX, y: aLbl.frame.maxY - padY, width: qLbl.frame.size.width, height: textFieldH)
catLbl.frame = CGRect(x: qLbl.frame.minX, y: aTfld.frame.maxY + padY, width: qLbl.frame.size.width, height: catLbl.frame.size.height + 5)
aTfld.frame = CGRect(x: qLbl.frame.minX, y: aLbl.frame.maxY, width: qLbl.frame.size.width, height: textFieldH)
catLbl.frame = CGRect(x: qLbl.frame.minX, y: aTfld.frame.maxY + padY, width: qLbl.frame.size.width, height: catLbl.frame.size.height)
catBtn.frame = CGRect(x: qLbl.frame.minX, y: catLbl.frame.maxY + padY, width: qLbl.frame.size.width/2, height: textFieldH)
createBtn.frame = CGRect(x: qLbl.frame.minX, y: catBtn.frame.maxY + textFieldH/2, width: qLbl.frame.size.width, height: textFieldH)
catTfld.frame = CGRect(x: qLbl.frame.minX, y: catLbl.frame.maxY - padY, width: qLbl.frame.size.width, height: textFieldH)
createBtn.frame = CGRect(x: qLbl.frame.minX, y: catBtn.frame.maxY + textFieldH, width: qLbl.frame.size.width, height: textFieldH)
catTfld.frame = CGRect(x: qLbl.frame.minX, y: catLbl.frame.maxY, width: qLbl.frame.size.width, height: textFieldH)
}

// MARK:- Private
Expand Down
54 changes: 52 additions & 2 deletions Memoria/CreateCardView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ class CreateCardView: UIView, UITextFieldDelegate, UIPickerViewDelegate, UIPicke
typealias CreateCardViewOnClose = (CreateCardView) -> Void
var onClose: CreateCardViewOnClose = { view in }

private var padY: CGFloat = 100.0
private var isKeyboardShowing: Bool = false

private lazy var titleLbl: UILabel! = {
let lbl = UILabel(frame: CGRect.zero)
lbl.font = Utils.logoFont()
lbl.textColor = Utils.textColor()
lbl.text = "Create Card"
lbl.sizeToFit()
lbl.frame.size.height += 5
return lbl
}()

Expand Down Expand Up @@ -71,6 +75,10 @@ class CreateCardView: UIView, UITextFieldDelegate, UIPickerViewDelegate, UIPicke
self.addSubview(closeBtn)
self.addSubview(picker)

NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShowNotification(notification:)), name: .UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHideNotification(notification:)), name: .UIKeyboardWillHide, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(applicationDidChangeStatusBarNotification(notification:)), name: .UIApplicationWillChangeStatusBarFrame, object: nil)

DataMgr.sharedInstance.fetchCreateCategories() { categories in
self.categories = categories
let cat: Category = Category(name: "Add new category", width: 0)
Expand All @@ -88,11 +96,10 @@ class CreateCardView: UIView, UITextFieldDelegate, UIPickerViewDelegate, UIPicke
let w = self.frame.size.width
let h = self.frame.size.height
let pad: CGFloat = 30.0
let padY: CGFloat = 100.0
gradientLayer.frame = self.bounds
card.frame = CGRect(x: pad, y: padY, width: w - pad * 2, height: h - padY/2)
closeBtn.frame = CGRect(x: w - (pad + btnS), y: padY - btnS, width: btnS, height: btnS)
titleLbl.frame = CGRect(x: pad, y: padY - (titleLbl.frame.size.height + 15), width: titleLbl.frame.size.width, height: titleLbl.frame.size.height + 5)
titleLbl.frame = CGRect(x: pad, y: closeBtn.frame.minY, width: titleLbl.frame.size.width, height: titleLbl.frame.size.height)
picker.frame = CGRect(x: pad, y: h - picker.frame.size.height, width: w - pad * 2, height: picker.frame.size.height)
}

Expand Down Expand Up @@ -123,6 +130,7 @@ class CreateCardView: UIView, UITextFieldDelegate, UIPickerViewDelegate, UIPicke
self.card.catBtn.isUserInteractionEnabled = false
self.card.createBtn.isUserInteractionEnabled = true
self.card.createBtn.isHidden = false
card.catTfld.becomeFirstResponder()
}

// MARK:- UITextFieldDelegate methods
Expand Down Expand Up @@ -161,6 +169,48 @@ class CreateCardView: UIView, UITextFieldDelegate, UIPickerViewDelegate, UIPicke
self.showPicker(show: false)
}

// MARK:- Keyboard notifications

func updateCardPositions() {

if isKeyboardShowing == false {
//default position
padY = 100
}
else {
if card.qTfld.isFirstResponder == true {
padY = 220
}
else if card.aTfld.isFirstResponder == true {
padY = 120
}
else if card.catTfld.isFirstResponder == true {
padY = 20
}
else {
padY = 100
}
}
//debugPrint("padY: \(padY)")
self.layoutSubviews()
}

func keyboardWillShowNotification(notification: NSNotification) {

isKeyboardShowing = !isKeyboardShowing
updateCardPositions()
}

func keyboardWillHideNotification(notification: NSNotification) {

isKeyboardShowing = !isKeyboardShowing
updateCardPositions()
}

func applicationDidChangeStatusBarNotification(notification: NSNotification) {
updateCardPositions()
}

// MARK:- Animations

func randomColors() -> [CGColor] {
Expand Down

0 comments on commit fdab58d

Please sign in to comment.