-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLabelCubeTransition.swift
More file actions
50 lines (39 loc) · 1.59 KB
/
LabelCubeTransition.swift
File metadata and controls
50 lines (39 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//
// LabelCubeTransition.swift
// FieldValidationLib
//
// Created by Mudith Chathuranga on 4/4/18.
// Copyright © 2018 Chathuranga. All rights reserved.
//
import Foundation
import UIKit
struct LabelCubeTransition {
static func cubeTransition(label: UILabel, text: String, direction: AnimationDirection) {
let nextLabel = UILabel(frame: label.frame)
nextLabel.text = text
nextLabel.font = label.font
nextLabel.textAlignment = label.textAlignment
nextLabel.textColor = label.textColor
nextLabel.backgroundColor = UIColor.clear//label.backgroundColor //Change Label Background Color
let nextLabelOffset = CGFloat(direction.rawValue) *
label.frame.size.height/2.0
nextLabel.transform = CGAffineTransform(scaleX: 1.0, y: 0.1).concatenating(
CGAffineTransform(translationX: 0.0, y: nextLabelOffset))
label.superview?.addSubview(nextLabel)
UIView.animate(withDuration: 2.0, delay: 0.0, options: .curveEaseOut,
animations: {
nextLabel.transform = CGAffineTransform.identity
label.transform = CGAffineTransform(scaleX: 1.0, y: 0.1).concatenating (
CGAffineTransform(translationX: 0.0, y: -nextLabelOffset))
}, completion: {_ in
label.text = nextLabel.text
label.transform = .identity
nextLabel.removeFromSuperview()
}
)
}
}
enum AnimationDirection: Int {
case fromBottom = 1
case fromTop = -1
}