Skip to content

Instantly share code, notes, and snippets.

@romainmenke
Last active November 10, 2015 17:17
Show Gist options
  • Save romainmenke/d5b7637f466e482095bf to your computer and use it in GitHub Desktop.
Save romainmenke/d5b7637f466e482095bf to your computer and use it in GitHub Desktop.
import UIKit
import XCPlayground
enum AnimatedTextUpdate {
case up
case down
case left
case right
}
class AnimationLabel : UIView {
var labelOne : UILabel
var labelTwo : UILabel
// reference to labelOne and labelTwo that is based on visibility
private weak var visibleLabel : UILabel!
private weak var inVisibleLabel : UILabel!
// implement all needed label attributes in a similar fashion
var numberOfLines : Int {
get {
return visibleLabel.numberOfLines
}
set(value) {
visibleLabel.numberOfLines = value
inVisibleLabel.numberOfLines = value
}
}
var text : String? {
get {
return visibleLabel.text
}
set(value) {
visibleLabel.text = value
}
}
override init(frame: CGRect) {
labelOne = UILabel(frame: CGRect(origin: CGPointZero, size: frame.size))
labelTwo = UILabel(frame: CGRect(origin: CGPoint(x: 0, y: frame.size.height), size: frame.size))
super.init(frame: frame)
labelOne.textAlignment = NSTextAlignment.Center
labelTwo.textAlignment = NSTextAlignment.Center
visibleLabel = labelOne
inVisibleLabel = labelTwo
self.addSubview(labelOne)
self.addSubview(labelTwo)
// hide stuff that is outside of the frame
self.clipsToBounds = true
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func updateTextAnimated(text updateText:String,animation: AnimatedTextUpdate,duration:Double) {
// late init of points
let invisibleOrigin : CGPoint
let invisibleDestination : CGPoint = CGPointZero
let visibleDestination : CGPoint
// switch the animation
switch animation {
case .down :
invisibleOrigin = CGPoint(x: 0, y: -visibleLabel.frame.size.height)
visibleDestination = CGPoint(x: 0, y: visibleLabel.frame.size.height)
case .up :
invisibleOrigin = CGPoint(x: 0, y: visibleLabel.frame.size.height)
visibleDestination = CGPoint(x: 0, y: -visibleLabel.frame.size.height)
case .right :
invisibleOrigin = CGPoint(x: -visibleLabel.frame.size.width, y: 0)
visibleDestination = CGPoint(x: visibleLabel.frame.size.width, y: 0)
case .left :
invisibleOrigin = CGPoint(x: visibleLabel.frame.size.width, y: 0)
visibleDestination = CGPoint(x: -visibleLabel.frame.size.width, y: 0)
}
// set correct start position and set text
inVisibleLabel.frame.origin = invisibleOrigin
inVisibleLabel.text = updateText
// animate
UIView.animateWithDuration(duration) { () -> Void in
self.visibleLabel.frame.origin = visibleDestination
self.inVisibleLabel.frame.origin = invisibleDestination
}
// switch label reference
let tempLabel = visibleLabel
visibleLabel = inVisibleLabel
inVisibleLabel = tempLabel
}
}
var test = AnimationLabel(frame: CGRect(origin: CGPointZero, size: CGSize(width: 200, height: 50)))
test.backgroundColor = UIColor.whiteColor()
test.text = "1"
test.updateTextAnimated(text: "2", animation: .down, duration: 1.0)
XCPlaygroundPage.currentPage.liveView = test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment