Commit c46898e0 by Daniel Dahan

updated animation when rotating to new orientation

parent d1ad100c
...@@ -101,7 +101,7 @@ class ViewController: UIViewController, TextFieldDelegate { ...@@ -101,7 +101,7 @@ class ViewController: UIViewController, TextFieldDelegate {
/// Programmatic update for the textField as it rotates. /// Programmatic update for the textField as it rotates.
override func willRotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) { override func willRotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) {
emailField.width = view.bounds.height emailField.width = view.bounds.height - 80
} }
/// General preparation statements. /// General preparation statements.
...@@ -133,7 +133,7 @@ class ViewController: UIViewController, TextFieldDelegate { ...@@ -133,7 +133,7 @@ class ViewController: UIViewController, TextFieldDelegate {
private func prepareNameField() { private func prepareNameField() {
nameField = MTextField() nameField = MTextField()
nameField.placeholder = "Email" nameField.placeholder = "Email"
nameField.detail = "Enter your email address." nameField.detail = "Enter your email address.yyypppggg"
nameField.clearButtonMode = .WhileEditing nameField.clearButtonMode = .WhileEditing
nameField.textAlignment = .Center nameField.textAlignment = .Center
nameField.text = "daniel@dahan" nameField.text = "daniel@dahan"
...@@ -146,15 +146,15 @@ class ViewController: UIViewController, TextFieldDelegate { ...@@ -146,15 +146,15 @@ class ViewController: UIViewController, TextFieldDelegate {
// Size the TextField to the maximum width, less 20 pixels on either side // Size the TextField to the maximum width, less 20 pixels on either side
// with a top margin of 100 pixels. // with a top margin of 100 pixels.
MaterialLayout.alignFromTop(view, child: nameField, top: 100) MaterialLayout.alignFromTop(view, child: nameField, top: 40)
MaterialLayout.alignToParentHorizontally(view, child: nameField, left: 40, right: 40) MaterialLayout.alignToParentHorizontally(view, child: nameField, left: 40, right: 40)
} }
/// Prepares the email TextField. /// Prepares the email TextField.
private func prepareEmailField() { private func prepareEmailField() {
emailField = MTextField(frame: CGRectMake(40, 200, view.bounds.width - 80, 32)) emailField = MTextField(frame: CGRectMake(40, 120, view.bounds.width - 80, 32))
emailField.placeholder = "Email" emailField.placeholder = "Email"
emailField.detail = "Error, incorrect email." emailField.detail = "Error, incorrect email.yyppggg"
emailField.delegate = self emailField.delegate = self
emailField.clearButtonMode = .WhileEditing emailField.clearButtonMode = .WhileEditing
...@@ -163,6 +163,9 @@ class ViewController: UIViewController, TextFieldDelegate { ...@@ -163,6 +163,9 @@ class ViewController: UIViewController, TextFieldDelegate {
emailField.dividerColor = MaterialColor.cyan.base emailField.dividerColor = MaterialColor.cyan.base
emailField.detailColor = MaterialColor.indigo.accent1 emailField.detailColor = MaterialColor.indigo.accent1
emailField.backgroundColor = MaterialColor.green.accent1
emailField.placeholderLabel.backgroundColor = MaterialColor.green.accent3
view.addSubview(emailField) view.addSubview(emailField)
} }
......
...@@ -200,6 +200,7 @@ public class MTextField : UITextField { ...@@ -200,6 +200,7 @@ public class MTextField : UITextField {
if let v: String = value { if let v: String = value {
detailLabel.attributedText = NSAttributedString(string: v, attributes: [NSForegroundColorAttributeName: detailColor]) detailLabel.attributedText = NSAttributedString(string: v, attributes: [NSForegroundColorAttributeName: detailColor])
} }
layoutDetailLabel()
} }
} }
...@@ -215,11 +216,12 @@ public class MTextField : UITextField { ...@@ -215,11 +216,12 @@ public class MTextField : UITextField {
/// Handles the textAlignment of the placeholderLabel. /// Handles the textAlignment of the placeholderLabel.
public override var textAlignment: NSTextAlignment { public override var textAlignment: NSTextAlignment {
get { get {
return placeholderLabel.textAlignment return super.textAlignment
} }
set(value) { set(value) {
super.textAlignment = value super.textAlignment = value
placeholderLabel.textAlignment = value placeholderLabel.textAlignment = value
detailLabel.textAlignment = value
} }
} }
...@@ -250,18 +252,14 @@ public class MTextField : UITextField { ...@@ -250,18 +252,14 @@ public class MTextField : UITextField {
public override func layoutSubviews() { public override func layoutSubviews() {
super.layoutSubviews() super.layoutSubviews()
if !editing && !animating { layoutPlaceholderLabel()
layoutPlaceholderLabel() layoutDetailLabel()
layoutDetailLabel()
}
} }
public override func layoutSublayersOfLayer(layer: CALayer) { public override func layoutSublayersOfLayer(layer: CALayer) {
super.layoutSublayersOfLayer(layer) super.layoutSublayersOfLayer(layer)
if self.layer == layer { if self.layer == layer {
if !editing { layoutDivider()
layoutDivider()
}
} }
} }
...@@ -357,26 +355,41 @@ public class MTextField : UITextField { ...@@ -357,26 +355,41 @@ public class MTextField : UITextField {
/// Layout the divider. /// Layout the divider.
public func layoutDivider() { public func layoutDivider() {
divider.frame = CGRectMake(0, height, width, dividerHeight) divider.frame = CGRectMake(0, height, width, editing ? dividerActiveHeight : dividerHeight)
} }
/// Layout the placeholderLabel. /// Layout the placeholderLabel.
public func layoutPlaceholderLabel() { public func layoutPlaceholderLabel() {
if true == text?.isEmpty { if !editing && !animating {
placeholderLabel.frame = bounds if true == text?.isEmpty {
} else if CGAffineTransformIsIdentity(placeholderLabel.transform) { placeholderLabel.frame = bounds
placeholderLabel.frame = bounds } else if CGAffineTransformIsIdentity(placeholderLabel.transform) {
placeholderLabel.transform = CGAffineTransformMakeScale(0.75, 0.75) placeholderLabel.frame = bounds
placeholderLabel.transform = CGAffineTransformMakeScale(0.75, 0.75)
switch textAlignment {
case .Left, .Natural:
placeholderLabel.frame.origin.x = 0
case .Right:
placeholderLabel.frame.origin.x = width - placeholderLabel.frame.width
default:break;
}
placeholderLabel.frame.origin.y = -placeholderLabel.frame.size.height
placeholderLabel.textColor = placeholderColor
}
} else if !animating {
placeholderLabel.frame.origin.x = 0 placeholderLabel.frame.origin.x = 0
placeholderLabel.frame.origin.y = -placeholderLabel.frame.size.height placeholderLabel.frame.size.width = width
placeholderLabel.textColor = placeholderColor
} }
} }
/// Layout the detailLabel. /// Layout the detailLabel.
public func layoutDetailLabel() { public func layoutDetailLabel() {
detailLabel.frame = CGRectMake(0, height + 8, width, 12) var h: CGFloat = 12
detailLabel.sizeToFit() if let v: String = detail {
let size: CGSize = detailLabel.font.stringSize(v, constrainedToWidth: Double(width))
h = size.height
}
detailLabel.frame = CGRectMake(0, height + 8, width, h)
} }
/// The animation for the divider when editing begins. /// The animation for the divider when editing begins.
...@@ -398,7 +411,13 @@ public class MTextField : UITextField { ...@@ -398,7 +411,13 @@ public class MTextField : UITextField {
UIView.animateWithDuration(0.15, animations: { [weak self] in UIView.animateWithDuration(0.15, animations: { [weak self] in
if let v: MTextField = self { if let v: MTextField = self {
v.placeholderLabel.transform = CGAffineTransformMakeScale(0.75, 0.75) v.placeholderLabel.transform = CGAffineTransformMakeScale(0.75, 0.75)
v.placeholderLabel.frame.origin.x = 0 switch v.textAlignment {
case .Left, .Natural:
v.placeholderLabel.frame.origin.x = 0
case .Right:
v.placeholderLabel.frame.origin.x = v.width - v.placeholderLabel.frame.width
default:break;
}
v.placeholderLabel.frame.origin.y = -v.placeholderLabel.frame.size.height v.placeholderLabel.frame.origin.y = -v.placeholderLabel.frame.size.height
v.placeholderLabel.textColor = v.placeholderActiveColor v.placeholderLabel.textColor = v.placeholderActiveColor
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment