Skip to content

Instantly share code, notes, and snippets.

@Pash237
Last active October 26, 2020 09:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pash237/7c3d46b1f7409f268be7d50f63aa672e to your computer and use it in GitHub Desktop.
Save Pash237/7c3d46b1f7409f268be7d50f63aa672e to your computer and use it in GitHub Desktop.
//
// TimeTicksView.swift
// MyAlarmVideo
//
// Created by Pavel Alexeev on 13.04.2020.
// Copyright © 2020 Pavel Alexeev. All rights reserved.
//
import Foundation
import UIKit
class TimeTicksView: UIView
{
var slices: [VideoSlice] {
didSet {
update()
}
}
private lazy var ticks = (0..<10).map {_ in
TimeTickView(frame: CGRect(x: 0, y: 0, width: width, height: 22))
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
init(frame: CGRect, slices: [VideoSlice]) {
self.slices = slices
super.init(frame: frame)
}
func update() {
let nearestHour = Date().nearestHour
var currentDate = nearestHour + 30*60
var currentY: CGFloat = 0
while currentY < height {
let localY = Self.yPosition(for: currentDate)
let globalY = convert(CGPoint(x: 0, y: localY), to: nil).y
currentY = localY
if globalY > 0 && localY >= 0 {
addTick(at: currentDate)
}
if globalY > UIScreen.main.bounds.height {
break
}
currentDate -= 30*60
}
}
private func availableTick() -> TimeTickView? {
for tick in ticks {
if !tick.isVisibleToUser {
return tick
}
}
print("No available tick!")
return nil
}
@discardableResult
private func addTick(at date: Date) -> UIView? {
// проверяем, может такой уже есть
for tick in ticks {
if tick.date == date {
return tick
}
}
// переиспользуем
if let tick = availableTick() {
tick.date = date
addSubview(tick)
return tick
}
return nil
}
private static func yPosition(for date: Date) -> CGFloat {
return date.timeIntervalToNow.toPoints()
}
class TimeTickView: UIView {
var date: Date = Date() {
didSet {
guard oldValue != date else {
return
}
y = TimeTicksView.yPosition(for: date) - 11
label.text = Self.dateFormatter.string(from: date)
if label.text == "00:00" {
dayLabel.isVisible = true
// отнимаем одну секунду, чтобы в 00:00 показывалось вчера
dayLabel.text = (date - 1).userFriendlyDateString
} else {
dayLabel.isVisible = false
}
}
}
private static let dateFormatter = DateFormatter(format: "HH:mm")
private var label = UILabel()
private var dayLabel = UILabel()
override init(frame: CGRect) {
super.init(frame: frame)
label.frame = CGRect(x: 0, y: 0, width: 44, height: 22)
label.lineBreakMode = .byClipping
label.font = .monospacedDigitSystemFont(ofSize: 14, weight: .regular)
label.textColor = .buttonDisabled
addSubview(label)
let tick = UIView(frame: CGRect(x: label.right, y: 11, width: frame.width - label.right, height: 1))
tick.backgroundColor = .timeTicks
tick.autoresizingMask = [.flexibleLeftMargin, .flexibleTopMargin, .flexibleBottomMargin]
addSubview(tick)
// выводим день напротив метки
dayLabel.frame = CGRect(x: tick.right + 16, y: label.y, width: 200, height: label.height)
dayLabel.autoresizingMask = [.flexibleLeftMargin, .flexibleTopMargin, .flexibleBottomMargin]
dayLabel.lineBreakMode = .byClipping
dayLabel.font = .systemFont(ofSize: 14)
dayLabel.textColor = label.textColor
addSubview(dayLabel)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment