Skip to content

Commit

Permalink
Fix long press event gesture not being recognized
Browse files Browse the repository at this point in the history
  • Loading branch information
richardtop committed Feb 18, 2020
1 parent 456b366 commit 318ef6c
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 66 deletions.
11 changes: 1 addition & 10 deletions Source/DayView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public protocol DayViewDelegate: AnyObject {
func dayView(dayView: DayView, didUpdate event: EventDescriptor)
}

public class DayView: UIView, EventViewDelegate, TimelinePagerViewDelegate {
public class DayView: UIView, TimelinePagerViewDelegate {

public weak var dataSource: EventDataSource? {
get {
Expand Down Expand Up @@ -145,15 +145,6 @@ public class DayView: UIView, EventViewDelegate, TimelinePagerViewDelegate {
timelinePagerView.endEventEditing()
}

// MARK: EventViewDelegate

public func eventViewDidTap(_ eventView: EventView) {
delegate?.dayViewDidSelectEventView(eventView)
}
public func eventViewDidLongPress(_ eventview: EventView) {
delegate?.dayViewDidLongPressEventView(eventview)
}

// MARK: TimelinePagerViewDelegate

public func timelinePagerDidSelectEventView(_ eventView: EventView) {
Expand Down
14 changes: 4 additions & 10 deletions Source/Timeline/AllDayView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
import UIKit

public class AllDayView: UIView {

internal weak var eventViewDelegate: EventViewDelegate?

var style = AllDayViewStyle()

let allDayLabelWidth: CGFloat = 53.0
Expand All @@ -16,6 +13,8 @@ public class AllDayView: UIView {
}
}

public private(set) var eventViews = [EventView]()

private lazy var textLabel: UILabel = {
let label = UILabel(frame: CGRect(x: 8.0, y: 4.0, width: allDayLabelWidth, height: 24.0))
label.text = localizedString("all-day")
Expand Down Expand Up @@ -87,13 +86,11 @@ public class AllDayView: UIView {

override init(frame: CGRect) {
super.init(frame: frame)

configure()
}

required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)

configure()
}

Expand Down Expand Up @@ -130,6 +127,7 @@ public class AllDayView: UIView {
}

// clear event views from scroll view
eventViews.removeAll()
scrollView.subviews.forEach { $0.removeFromSuperview() }

if self.events.count == 0 { return }
Expand All @@ -146,7 +144,6 @@ public class AllDayView: UIView {
// create event
let eventView = EventView(frame: CGRect.zero)
eventView.updateWithDescriptor(event: anEventDescriptor)
eventView.delegate = self.eventViewDelegate
eventView.heightAnchor.constraint(equalToConstant: allDayEventHeight).isActive = true

// create horz stack view if index % 2 == 0
Expand All @@ -162,6 +159,7 @@ public class AllDayView: UIView {

// add eventView to horz. stack view
horizontalStackView.addArrangedSubview(eventView)
eventViews.append(eventView)
}

// add vert. stack view inside, pin vert. stack view, update content view by the number of horz. stack views
Expand All @@ -177,8 +175,4 @@ public class AllDayView: UIView {
verticalStackViewHeightConstraint.priority = UILayoutPriority(rawValue: 999)
verticalStackViewHeightConstraint.isActive = true
}

// MARK: - LIFE CYCLE

}

26 changes: 0 additions & 26 deletions Source/Timeline/EventView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,7 @@ import UIKit
import DateToolsSwift
import Neon

public protocol EventViewDelegate: AnyObject {
func eventViewDidTap(_ eventView: EventView)
func eventViewDidLongPress(_ eventview: EventView)
}

open class EventView: UIView {

weak var delegate: EventViewDelegate?
public var descriptor: EventDescriptor?

public var color = UIColor.lightGray
Expand All @@ -26,12 +19,6 @@ open class EventView: UIView {
return view
}()

lazy var tapGestureRecognizer = UITapGestureRecognizer(target: self,
action: #selector(tap))

lazy var longPressGestureRecognizer = UILongPressGestureRecognizer(target: self,
action: #selector(longPress))

/// Resize Handle views showing up when editing the event.
/// The top handle has a tag of `0` and the bottom has a tag of `1`
public lazy var eventResizeHandles = [EventResizeHandleView(), EventResizeHandleView()]
Expand All @@ -48,8 +35,6 @@ open class EventView: UIView {

func configure() {
clipsToBounds = false
[tapGestureRecognizer, longPressGestureRecognizer].forEach {addGestureRecognizer($0)}

color = tintColor
addSubview(textView)

Expand Down Expand Up @@ -79,16 +64,6 @@ open class EventView: UIView {
setNeedsLayout()
}

@objc func tap() {
delegate?.eventViewDidTap(self)
}

@objc func longPress(_ sender: UILongPressGestureRecognizer) {
if sender.state == .began {
delegate?.eventViewDidLongPress(self)
}
}

/**
Custom implementation of the hitTest method is needed for the tap gesture recognizers
located in the ResizeHandleView to work.
Expand Down Expand Up @@ -186,4 +161,3 @@ open class EventView: UIView {
completion: nil)
}
}

16 changes: 7 additions & 9 deletions Source/Timeline/TimelinePagerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public protocol TimelinePagerViewDelegate: AnyObject {
func timelinePager(timelinePager: TimelinePagerView, didUpdate event: EventDescriptor)
}

public class TimelinePagerView: UIView, UIGestureRecognizerDelegate, UIScrollViewDelegate, DayViewStateUpdating, UIPageViewControllerDataSource, UIPageViewControllerDelegate, TimelineViewDelegate, EventViewDelegate {
public class TimelinePagerView: UIView, UIGestureRecognizerDelegate, UIScrollViewDelegate, DayViewStateUpdating, UIPageViewControllerDataSource, UIPageViewControllerDelegate, TimelineViewDelegate {

public weak var dataSource: EventDataSource?
public weak var delegate: TimelinePagerViewDelegate?
Expand Down Expand Up @@ -135,7 +135,6 @@ public class TimelinePagerView: UIView, UIGestureRecognizerDelegate, UIScrollVie
let timeline = controller.timeline
timeline.longPressGestureRecognizer.addTarget(self, action: #selector(timelineDidLongPress(_:)))
timeline.delegate = self
timeline.eventViewDelegate = self
timeline.calendar = calendar
timeline.date = date.dateOnly(calendar: calendar)
controller.container.delegate = self
Expand Down Expand Up @@ -455,13 +454,12 @@ public class TimelinePagerView: UIView, UIGestureRecognizerDelegate, UIScrollVie
public func timelineView(_ timelineView: TimelineView, didLongPressAt date: Date) {
delegate?.timelinePager(timelinePager: self, didLongPressTimelineAt: date)
}

// MARK: EventViewDelegate

public func eventViewDidTap(_ eventView: EventView) {
delegate?.timelinePagerDidSelectEventView(eventView)

public func timelineView(_ timelineView: TimelineView, didTap event: EventView) {
delegate?.timelinePagerDidSelectEventView(event)
}
public func eventViewDidLongPress(_ eventview: EventView) {
delegate?.timelinePagerDidLongPressEventView(eventview)

public func timelineView(_ timelineView: TimelineView, didLongPress event: EventView) {
delegate?.timelinePagerDidLongPressEventView(event)
}
}
44 changes: 33 additions & 11 deletions Source/Timeline/TimelineView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@ import DateToolsSwift
public protocol TimelineViewDelegate: AnyObject {
func timelineView(_ timelineView: TimelineView, didTapAt date: Date)
func timelineView(_ timelineView: TimelineView, didLongPressAt date: Date)
func timelineView(_ timelineView: TimelineView, didTap event: EventView)
func timelineView(_ timelineView: TimelineView, didLongPress event: EventView)
}

public class TimelineView: UIView {
public weak var delegate: TimelineViewDelegate?
public weak var eventViewDelegate: EventViewDelegate? {
didSet {
self.allDayView.eventViewDelegate = eventViewDelegate
}
}

public var date = Date() {
didSet {
Expand Down Expand Up @@ -174,19 +171,45 @@ public class TimelineView: UIView {

// MARK: - Event Handling

@objc func longPress(_ gestureRecognizer: UILongPressGestureRecognizer) {
@objc private func longPress(_ gestureRecognizer: UILongPressGestureRecognizer) {
if (gestureRecognizer.state == .began) {
// Get timeslot of gesture location
let pressedLocation = gestureRecognizer.location(in: self)
delegate?.timelineView(self, didLongPressAt: yToDate(pressedLocation.y))
if let eventView = findEventView(at: pressedLocation) {
delegate?.timelineView(self, didLongPress: eventView)
} else {
delegate?.timelineView(self, didLongPressAt: yToDate(pressedLocation.y))
}
}
}

@objc func tap(_ sender: UITapGestureRecognizer) {
@objc private func tap(_ sender: UITapGestureRecognizer) {
let pressedLocation = sender.location(in: self)
delegate?.timelineView(self, didTapAt: yToDate(pressedLocation.y))
if let eventView = findEventView(at: pressedLocation) {
delegate?.timelineView(self, didTap: eventView)
} else {
delegate?.timelineView(self, didTapAt: yToDate(pressedLocation.y))
}
}

private func findEventView(at point: CGPoint) -> EventView? {
for eventView in eventViews {
let frame = eventView.frame
if frame.contains(point) {
return eventView
}
}

for eventView in allDayView.eventViews {
let frame = eventView.convert(eventView.bounds, to: self)
if frame.contains(point) {
return eventView
}
}
return nil
}


/**
Custom implementation of the hitTest method is needed for the tap gesture recognizers
located in the AllDayView to work.
Expand Down Expand Up @@ -420,7 +443,6 @@ public class TimelineView: UIView {
eventViews.removeAll()
for _ in regularLayoutAttributes {
let newView = pool.dequeue()
newView.delegate = eventViewDelegate
if newView.superview == nil {
addSubview(newView)
}
Expand Down

0 comments on commit 318ef6c

Please sign in to comment.