Skip to content

Commit 9bebeba

Browse files
committed
Make panning area bigger for playhead view
1 parent b9d4dbe commit 9bebeba

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

MIDITimeTableView/Source/MIDITimeTablePlayheadView.swift

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ public class MIDITimeTablePlayheadView: UIView {
5555
private var imageView: UIImageView?
5656
/// Delegate of playhead.
5757
public weak var delegate: MIDITimeTablePlayheadViewDelegate?
58+
/// The view for panning.
59+
private var panningView = UIView()
60+
/// The hit are offset for panning.
61+
public var panningOffset: CGFloat = 20
5862

5963
// MARK: Init
6064

@@ -73,7 +77,8 @@ public class MIDITimeTablePlayheadView: UIView {
7377
}
7478

7579
private func commonInit() {
76-
addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(didPan(pan:))))
80+
panningView.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(didPan(pan:))))
81+
addSubview(panningView)
7782
layer.addSublayer(lineLayer)
7883
layer.addSublayer(shapeLayer)
7984
}
@@ -82,6 +87,7 @@ public class MIDITimeTablePlayheadView: UIView {
8287

8388
public override func layoutSubviews() {
8489
super.layoutSubviews()
90+
panningView.frame = CGRect(x: -panningOffset, y: -panningOffset, width: frame.size.width + (panningOffset * 2), height: frame.size.height + (panningOffset * 2))
8591
lineLayer.frame.size = CGSize(width: lineWidth, height: lineHeight + (frame.height / 2))
8692
lineLayer.frame.origin.y = frame.height - (frame.height / 2)
8793
lineLayer.position.x = frame.width / 2
@@ -91,6 +97,21 @@ public class MIDITimeTablePlayheadView: UIView {
9197
drawShapeLayer()
9298
}
9399

100+
public override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
101+
if clipsToBounds || isHidden || alpha == 0 {
102+
return nil
103+
}
104+
105+
for subview in subviews.reversed() {
106+
let subPoint = subview.convert(point, from: self)
107+
if let result = subview.hitTest(subPoint, with: event) {
108+
return result
109+
}
110+
}
111+
112+
return nil
113+
}
114+
94115
private func updatePosition() {
95116
frame = CGRect(
96117
x: rowHeaderWidth + (CGFloat(position) * measureBeatWidth) - (frame.size.width / 2),

MIDITimeTableView/Source/MIDITimeTableView.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ open class MIDITimeTableView: UIScrollView, MIDITimeTableCellViewDelegate, MIDIT
309309
rangeheadView.lineHeight = contentSize.height - measureHeight
310310
rangeheadView.measureBeatWidth = measureWidth / CGFloat(measureView.beatCount)
311311
rangeheadView.isHidden = !showsPlayhead
312+
bringSubview(toFront: rangeheadView)
312313

313314
// Grid layer
314315
gridLayer.rowCount = rowHeaderCellViews.count
@@ -727,8 +728,10 @@ open class MIDITimeTableView: UIScrollView, MIDITimeTableCellViewDelegate, MIDIT
727728
// Horizontal move
728729
if translation.x > subbeatWidth, playheadView.frame.maxX < contentSize.width {
729730
playheadView.position += 0.25
731+
panGestureRecognizer.setTranslation(CGPoint(x: 0, y: translation.y), in: self)
730732
} else if translation.x < -subbeatWidth, playheadView.frame.minX > headerCellWidth {
731733
playheadView.position -= 0.25
734+
panGestureRecognizer.setTranslation(CGPoint(x: 0, y: translation.y), in: self)
732735
}
733736

734737
// Fire delegate
@@ -739,9 +742,6 @@ open class MIDITimeTableView: UIScrollView, MIDITimeTableCellViewDelegate, MIDIT
739742
timeTableDelegate?.midiTimeTableView(self, didUpdateRangeHead: rangeheadView.position)
740743
}
741744
}
742-
743-
// Reset translation
744-
panGestureRecognizer.setTranslation(.zero, in: self)
745745
}
746746

747747
// MARK: MIDITimeTableHistoryDelegate

0 commit comments

Comments
 (0)