Skip to content

Commit 1446745

Browse files
committed
Support for animation completion blocks
1 parent 8d94104 commit 1446745

12 files changed

+82
-24
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## Version 1.2
4+
* Swift 2.2 support
5+
* Support for animation completion blocks
6+
37
## Version 1.1.1
48
* Fixes bug #57 in Yoshiko
59

TextFieldEffects.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Pod::Spec.new do |s|
1616
#
1717

1818
s.name = "TextFieldEffects"
19-
s.version = "1.1.1"
19+
s.version = "1.2"
2020
s.summary = "Custom drop in UITextFields effects."
2121

2222
s.description = <<-DESC

TextFieldEffects/TextFieldEffects/AkiraTextField.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import UIKit
1313
*/
1414
@IBDesignable public class AkiraTextField : TextFieldEffects {
1515

16-
private let borderSize : (active: CGFloat, inactive: CGFloat) = (1, 2)
16+
private let borderSize: (active: CGFloat, inactive: CGFloat) = (1, 2)
1717
private let borderLayer = CALayer()
1818
private let textFieldInsets = CGPoint(x: 6, y: 0)
1919
private let placeHolderInsets = CGPoint(x: 6, y: 0)
@@ -77,13 +77,17 @@ import UIKit
7777
UIView.animateWithDuration(0.3, animations: {
7878
self.updateBorder()
7979
self.updatePlaceholder()
80+
}, completion: { _ in
81+
self.animationCompletionHandler?(type: .TextEntry)
8082
})
8183
}
8284

8385
override public func animateViewsForTextDisplay() {
8486
UIView.animateWithDuration(0.3, animations: {
8587
self.updateBorder()
8688
self.updatePlaceholder()
89+
}, completion: { _ in
90+
self.animationCompletionHandler?(type: .TextDisplay)
8791
})
8892
}
8993

TextFieldEffects/TextFieldEffects/HoshiTextField.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@ import UIKit
9898
UIView.animateWithDuration(0.3, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 1.0, options: .BeginFromCurrentState, animations: ({
9999
self.placeholderLabel.frame.origin = CGPoint(x: 10, y: self.placeholderLabel.frame.origin.y)
100100
self.placeholderLabel.alpha = 0
101-
}), completion:nil)
101+
}), completion: { _ in
102+
self.animationCompletionHandler?(type: .TextEntry)
103+
})
102104
}
103105

104106
layoutPlaceholderInTextRect()
@@ -108,15 +110,17 @@ import UIKit
108110
self.placeholderLabel.alpha = 0.5
109111
})
110112

111-
activeBorderLayer.frame = self.rectForBorder(self.borderThickness.active, isFilled: true)
113+
activeBorderLayer.frame = rectForBorder(borderThickness.active, isFilled: true)
112114
}
113115

114116
override public func animateViewsForTextDisplay() {
115117
if text!.isEmpty {
116118
UIView.animateWithDuration(0.35, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 2.0, options: UIViewAnimationOptions.BeginFromCurrentState, animations: ({
117119
self.layoutPlaceholderInTextRect()
118120
self.placeholderLabel.alpha = 1
119-
}), completion: nil)
121+
}), completion: { _ in
122+
self.animationCompletionHandler?(type: .TextDisplay)
123+
})
120124

121125
self.activeBorderLayer.frame = self.rectForBorder(self.borderThickness.active, isFilled: false)
122126
}

TextFieldEffects/TextFieldEffects/IsaoTextField.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,15 +150,17 @@ import UIKit
150150
UIView.animateWithDuration(0.15, animations: {
151151
self.placeholderLabel.transform = CGAffineTransformMakeTranslation(0, -yOffset)
152152
self.placeholderLabel.alpha = 0
153-
}) { (completed) in
153+
}) { _ in
154154
self.placeholderLabel.transform = CGAffineTransformIdentity
155155
self.placeholderLabel.transform = CGAffineTransformMakeTranslation(0, yOffset)
156156

157157
UIView.animateWithDuration(0.15, animations: {
158158
self.placeholderLabel.textColor = color
159159
self.placeholderLabel.transform = CGAffineTransformIdentity
160160
self.placeholderLabel.alpha = 1
161-
})
161+
}) { _ in
162+
self.animationCompletionHandler?(type: self.isFirstResponder() ? .TextEntry : .TextDisplay)
163+
}
162164
}
163165
}
164166

TextFieldEffects/TextFieldEffects/JiroTextField.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,19 @@ import UIKit
8787
self.placeholderLabel.frame.origin = CGPoint(x: self.placeholderInsets.x, y: self.borderLayer.frame.origin.y - self.placeholderLabel.bounds.height)
8888
self.borderLayer.frame = self.rectForBorder(self.borderThickness, isFilled: true)
8989

90-
}), completion:nil)
90+
}), completion: { _ in
91+
self.animationCompletionHandler?(type: .TextEntry)
92+
})
9193
}
9294

9395
override public func animateViewsForTextDisplay() {
9496
if text!.isEmpty {
9597
UIView.animateWithDuration(0.35, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 2.0, options: .BeginFromCurrentState, animations: ({
9698
self.layoutPlaceholderInTextRect()
9799
self.placeholderLabel.alpha = 1
98-
}), completion: nil)
100+
}), completion: { _ in
101+
self.animationCompletionHandler?(type: .TextDisplay)
102+
})
99103

100104
borderLayer.frame = rectForBorder(borderThickness, isFilled: false)
101105
}

TextFieldEffects/TextFieldEffects/KaedeTextField.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ import UIKit
9090

9191
UIView.animateWithDuration(0.45, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 1.5, options: .BeginFromCurrentState, animations: ({
9292
self.foregroundView.frame.origin = CGPoint(x: self.frame.size.width * 0.6, y: 0)
93-
}), completion: nil)
93+
}), completion: { _ in
94+
self.animationCompletionHandler?(type: .TextEntry)
95+
})
9496
}
9597

9698
override public func animateViewsForTextDisplay() {
@@ -101,7 +103,9 @@ import UIKit
101103

102104
UIView.animateWithDuration(0.3, delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 2.0, options: .BeginFromCurrentState, animations: ({
103105
self.foregroundView.frame.origin = CGPointZero
104-
}), completion: nil)
106+
}), completion: { _ in
107+
self.animationCompletionHandler?(type: .TextDisplay)
108+
})
105109
}
106110
}
107111

TextFieldEffects/TextFieldEffects/MadokaTextField.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ import UIKit
8888
let scale = CGAffineTransformMakeScale(0.9, 0.9)
8989

9090
self.placeholderLabel.transform = CGAffineTransformConcat(translate, scale)
91-
})
91+
}) { _ in
92+
self.animationCompletionHandler?(type: .TextEntry)
93+
}
9294
}
9395

9496
override public func animateViewsForTextDisplay() {
@@ -97,7 +99,9 @@ import UIKit
9799

98100
UIView.animateWithDuration(0.3, animations: {
99101
self.placeholderLabel.transform = CGAffineTransformIdentity
100-
})
102+
}) { _ in
103+
self.animationCompletionHandler?(type: .TextDisplay)
104+
}
101105
}
102106
}
103107

TextFieldEffects/TextFieldEffects/MinoruTextField.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ import UIKit
8585
borderLayer.shadowColor = textColor?.CGColor
8686
borderLayer.shadowOpacity = 0.5
8787
borderLayer.shadowRadius = 1
88+
89+
animationCompletionHandler?(type: .TextEntry)
8890
}
8991

9092
override public func animateViewsForTextDisplay() {
@@ -94,6 +96,8 @@ import UIKit
9496
borderLayer.shadowColor = nil
9597
borderLayer.shadowOpacity = 0
9698
borderLayer.shadowRadius = 0
99+
100+
animationCompletionHandler?(type: .TextDisplay)
97101
}
98102

99103
// MARK: - Private

TextFieldEffects/TextFieldEffects/TextFieldsEffects.swift

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,22 @@ A TextFieldEffects object is a control that displays editable text and contains
2222
*/
2323
public class TextFieldEffects : UITextField {
2424

25+
/**
26+
The type of animatino a TextFieldEffect can perform.
27+
28+
- TextEntry: animation that takes effect when the textfield has focus.
29+
- TextDisplay: animation that takes effect when the textfield loses focus.
30+
*/
31+
public enum AnimationType: Int {
32+
case TextEntry
33+
case TextDisplay
34+
}
35+
36+
/**
37+
Closure executed when an animation has been completed.
38+
*/
39+
public typealias AnimationCompletionHandler = (type: AnimationType)->()
40+
2541
/**
2642
UILabel that holds all the placeholder information
2743
*/
@@ -41,6 +57,11 @@ public class TextFieldEffects : UITextField {
4157
fatalError("\(#function) must be overridden")
4258
}
4359

60+
/**
61+
The animation completion handler is the best place to be notified when the text field animation has ended.
62+
*/
63+
public var animationCompletionHandler: AnimationCompletionHandler?
64+
4465
/**
4566
Draws the receiver’s image within the passed-in rectangle.
4667

@@ -78,9 +99,9 @@ public class TextFieldEffects : UITextField {
7899

79100
override public func willMoveToSuperview(newSuperview: UIView!) {
80101
if newSuperview != nil {
81-
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(TextFieldEffects.textFieldDidEndEditing), name:UITextFieldTextDidEndEditingNotification, object: self)
102+
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(textFieldDidEndEditing), name: UITextFieldTextDidEndEditingNotification, object: self)
82103

83-
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(TextFieldEffects.textFieldDidBeginEditing), name:UITextFieldTextDidBeginEditingNotification, object: self)
104+
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(textFieldDidBeginEditing), name: UITextFieldTextDidBeginEditingNotification, object: self)
84105
} else {
85106
NSNotificationCenter.defaultCenter().removeObserver(self)
86107
}

0 commit comments

Comments
 (0)