1

I'm currently trying to create an app that uses the TimelinePagerView from the CalendarKit library, but I cannot add the EventView to the TimelinePagerView. How can I add an EventView to the TimelinePagerView?

I will provide any information you need.

Here's the code I made based on the sample code.

import UIKit
import CalendarKit

class ViewController: UIViewController, EventDataSource {
    
    @IBOutlet weak var timelinePagerView: TimelinePagerView!
    
    let date = Date()
    let eventData = ["event1", "event2", "event3", "event4"]
    
    var timelineStyle = TimelineStyle()
    var generatedEvents = [EventDescriptor]()
    var alreadyGeneratedSet = Set<Date>()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        timelineStyle.eventGap = 1
        timelinePagerView.updateStyle(timelineStyle)
        timelinePagerView.autoScrollToFirstEvent = true
        timelinePagerView.reloadData()
    }
    
    func eventsForDate(_ date: Date) -> [CalendarKit.EventDescriptor] {
        if !alreadyGeneratedSet.contains(date) {
            alreadyGeneratedSet.insert(date)
            generatedEvents.append(contentsOf: generateEventsForDate(date))
        }
        return generatedEvents
    }
    
    private func generateEventsForDate(_ date: Date) -> [EventDescriptor] {
        let workingDate1 = Calendar.current.date(byAdding: .hour, value: 9, to: date)!
        let workingDate2 = Calendar.current.date(byAdding: .hour, value: 11, to: date)!
        let workingDate3 = Calendar.current.date(byAdding: .hour, value: 13, to: date)!
        let workingDate4 = Calendar.current.date(byAdding: .hour, value: 17, to: date)!
        var events = [Event]()
        
        for i in 0...3 {
            let event = Event()
            
            if i == 0 {
                event.dateInterval = DateInterval(start: workingDate1, duration: 120 * 60)
            } else if i == 1 {
                event.dateInterval = DateInterval(start: workingDate2, duration: 60 * 60)
            } else if i == 2 {
                event.dateInterval = DateInterval(start: workingDate3, duration: 240 * 60)
            } else if i == 3 {
                event.dateInterval = DateInterval(start: workingDate4, duration: 60 * 60)
            }
            
            event.text = eventData[i]
            switch (i) {
            case 0,1,3:
                event.color = .red
            default:
                event.color = .blue
            }
            event.isAllDay = false
            event.lineBreakMode = .byTruncatingTail
            
            events.append(event)
            
            event.userInfo = String(i)
        }
        return events
    }
}
Kawboy442
  • 11
  • 3
  • Have you looked at this sample app? https://github.com/richardtop/CalendarApp – Richard Topchii Aug 05 '23 at 07:05
  • 1
    Yes, the sample here is a class that inherits from `DayViewController`, and what I would like to create is to embed a `TimelinePagerView` as part of the layout and display the schedule in it, is this possible? – Kawboy442 Aug 07 '23 at 00:42
  • Yes, it's possible and I've seen these implementations being done. TimelinePagerView is highly integrated with the rest of the CalendarKit, so ideally, you'd need to look at the upper layer, i.e. DayView and how it handles the TimelinePagerView, implement it's delegate/datasource methods. An alternative would be to use just the TimelineView if you don't need paging. – Richard Topchii Aug 12 '23 at 15:44

0 Answers0