Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save johndpope/4897dc5070abce8637886fe9310b9b1b to your computer and use it in GitHub Desktop.
Save johndpope/4897dc5070abce8637886fe9310b9b1b to your computer and use it in GitHub Desktop.
import Foundation
import UIKit
import SnapKit
import Material
class OnboardingSuperstarCollectionViewCell:CollectionViewCell {
static let ID = "OnboardingSuperstarCollectionViewCell"
lazy var heroImageView:UIImageView = {
let iv = UIImageView(frame:.zero)
iv.image = UIImage(named:"placeholder")
return iv
}()
let nameLabel = UILabel(frame: .zero)
let rotatingLabel = RotatingTextLabel()
var superStarId:String = ""
override func layoutSubviews() {
super.layoutSubviews()
}
override func prepareForReuse() {
super.prepareForReuse()
self.rotatingLabel.prepareForReuse()
}
override init(frame: CGRect) {
super.init(frame:frame)
self.contentView.backgroundColor = wweColors.onyx
self.backgroundColor = wweColors.rangoonGreen
// Dark Box container / inset border
let darkContainer = UIView(frame:.zero)
self.contentView.addSubview(darkContainer)
darkContainer.backgroundColor = wweColors.jungleGreen
darkContainer.snp.remakeConstraints { (make) -> Void in
make.left.equalToSuperview()
make.top.equalToSuperview()
make.height.equalTo(94)
make.edges.equalToSuperview().inset(UIEdgeInsetsMake(10, 10, 30, 10))
}
// hero Image
darkContainer.addSubview(self.heroImageView)
self.heroImageView.snp.remakeConstraints { (make) -> Void in
make.width.equalToSuperview()
make.height.equalTo(84) // TODO calculate this dynamically.
make.top.equalTo(darkContainer.snp.top)
make.left.equalTo(darkContainer.snp.left)
}
self.heroImageView.contentMode = .scaleAspectFit
darkContainer.addSubview(rotatingLabel)
rotatingLabel.snp.remakeConstraints { (make) -> Void in
make.left.width.equalToSuperview()
make.top.equalTo(self.heroImageView.snp.bottom)
make.height.equalTo(20)
}
}
func configureCell(superStar:WWESuperstar){
rotatingLabel.setText(text: superStar.name)
//self.nameLabel.text = superStar.name
self.heroImageView.loadImageUsingCacheWithUrlString(urlString: superStar.imageUrl)
self.superStarId = superStar.id
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
import Foundation
import SnapKit
import Material
import Dotzu
import TweenKit
class RotatingTextLabel: UIView {
let scheduler = ActionScheduler()
private let label: UILabel = UILabel()
var isCycling = false
var labelXoffset:CGFloat = 16
deinit{
scheduler.removeAll()
self.label.snp.removeConstraints()
}
func prepareForReuse(){
self.isCycling = false
scheduler.removeAll()
resetLayout()
}
override init(frame: CGRect) {
super.init(frame: frame)
self.setup()
}
convenience init() {
self.init(frame: .zero)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.setup()
}
func resetLayout(){
self.label.snp.remakeConstraints { (make) -> Void in
make.top.height.equalToSuperview()
make.left.equalToSuperview().offset(labelXoffset)
}
}
func setup() {
self.backgroundColor = .clear
self.addSubview(self.label)
self.resetLayout()
label.font = RobotoFont.medium(with: 15)
label.textColor = .white
label.backgroundColor = .clear
self.clipsToBounds = true
}
//MARK: - Public functions
func setText(text: String?) {
self.label.text = text
label.sizeToFit()
// sanity check 1
if (self.isCycling){
return;
}
// sanity check 2 - is this cell being recycled?
scheduler.removeAll()
// only cycle when label is larger than width of screen
if(label.frame.size.width + 32 > onboardingSuperStarSize.width){
self.firstMoveLeftOffScreen()
self.startCyclingFromRightOfScreen()
}
}
func updateUpdateConstraint(offset:CGFloat){
self.label.snp.updateConstraints { (make) -> Void in
make.left.equalTo(offset)
}
}
func firstMoveLeftOffScreen(){
let moveLeft = InterpolationAction(from: CGFloat(self.labelXoffset),
to: -CGFloat(self.label.width),
duration: 3,
easing: .linear) {
[unowned self] in
self.updateUpdateConstraint(offset: $0)
}
scheduler.run(action: moveLeft)
}
func startCyclingFromRightOfScreen() {
if(!isCycling == true){
isCycling = true
let cycleLeftAgain = InterpolationAction(from: CGFloat(onboardingSuperStarSize.width),
to: -CGFloat(self.label.width),
duration: 10,
easing: .linear) {
[unowned self] in
self.updateUpdateConstraint(offset: $0)
}
let cycleSequence = ActionSequence(actions: cycleLeftAgain )
let action = cycleSequence.repeatedForever()
scheduler.run(action: action)
}
}
}
public extension ActionSequence {
public func repeatedForever() -> RepeatForeverAction {
return RepeatForeverAction(action: self)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment