Skip to content

Instantly share code, notes, and snippets.

@gmoraleda
Last active November 15, 2018 13:00
Show Gist options
  • Save gmoraleda/5bddf0f9d54c90a826ef2d625d5ee43f to your computer and use it in GitHub Desktop.
Save gmoraleda/5bddf0f9d54c90a826ef2d625d5ee43f to your computer and use it in GitHub Desktop.
func getOffsetForDate(date: Date) -> CGFloat {
// dayViewHeight being the height of the view that containts the hour label and the line separator
let components = Calendar.current.dateComponents([.minute, .hour], from: date)
let hourOffset = CGFloat(components.hour ?? 0) * (dayViewHeight + Sizes.gridLineSeparation)
let minuteOffset = ceilToPixelsAccuracy(CGFloat(components.minute ?? 0) / 60 * (dayViewHeight + Sizes.gridLineSeparation))
return hourOffset + minuteOffset + ceilToPixelsAccuracy(dayViewHeight / 2)
}
// Function to ceil according to the scaling factor
private func ceilToPixelsAccuracy(_ dots: CGFloat) -> CGFloat {
return ceil(dots * UIScreen.main.scale) / UIScreen.main.scale
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment