Last active
May 16, 2018 06:11
-
-
Save malcolmkmd/abed786e8463552502cd08701172e83d to your computer and use it in GitHub Desktop.
UnderlineAnimator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
public class MDVUnderlineAnimator: MDVTabBarAnimatable { | |
private var underlineColor: UIColor = .clear | |
private var underlineHeight: CGFloat = 0 | |
private var xPosition: CGFloat = 0 | |
private var yPositionOffset: CGFloat = 0 | |
private var containerWidth: CGFloat = 0 | |
private var underlineView: UIView = UIView(frame: .zero) | |
private let duration: Double | |
private let delay: Double | |
private let damping: CGFloat | |
private let velocity: CGFloat | |
public init(withUnderlineColor underlineColor: UIColor = .red, | |
underlineHeight: CGFloat = 8, | |
yPositionOffset: CGFloat = 0, | |
duration: Double = 0.3, | |
delay: Double = 0, | |
damping: CGFloat = 0.5, | |
velocity: CGFloat = 0.6){ | |
self.underlineColor = underlineColor | |
self.underlineHeight = underlineHeight | |
self.yPositionOffset = yPositionOffset | |
self.damping = damping | |
self.duration = duration | |
self.delay = delay | |
self.velocity = velocity | |
} | |
public func prepareForAnimation(onMDVTabBar tabBar: UIView, withContainers containers: [MDVTabBarContainer], andInitialIndex initialIndex: Int) { | |
xPosition = CGFloat(containers[initialIndex].frame.origin.x) | |
let yPosition = CGFloat(tabBar.frame.height - underlineHeight) | |
containerWidth = containers[initialIndex].frame.width | |
self.underlineView = UIView(frame: CGRect(x: xPosition, | |
y: yPosition, | |
width: containerWidth, | |
height: self.underlineHeight)) | |
self.underlineView.backgroundColor = self.underlineColor | |
tabBar.insertSubview(self.underlineView, at: 0) | |
} | |
public func performAnimation(fromIndex: Int, | |
toIndex: Int, | |
onMDVTabBar tabBar: UIView, | |
withContainers containers: [MDVTabBarContainer], | |
completion: @escaping () -> Void) { | |
UIView.animate(withDuration: duration, | |
delay: delay, | |
usingSpringWithDamping: damping, | |
initialSpringVelocity: velocity, | |
options: .curveLinear, | |
animations: { | |
self.xPosition = CGFloat(containers[toIndex].frame.origin.x) | |
self.underlineView.frame.origin.x = self.xPosition | |
completion() | |
}) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment