Skip to content

Commit

Permalink
Make panning area bigger for playhead view
Browse files Browse the repository at this point in the history
  • Loading branch information
cemolcay committed Feb 15, 2018
1 parent b9d4dbe commit 9bebeba
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
23 changes: 22 additions & 1 deletion MIDITimeTableView/Source/MIDITimeTablePlayheadView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ public class MIDITimeTablePlayheadView: UIView {
private var imageView: UIImageView?
/// Delegate of playhead.
public weak var delegate: MIDITimeTablePlayheadViewDelegate?
/// The view for panning.
private var panningView = UIView()
/// The hit are offset for panning.
public var panningOffset: CGFloat = 20

// MARK: Init

Expand All @@ -73,7 +77,8 @@ public class MIDITimeTablePlayheadView: UIView {
}

private func commonInit() {
addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(didPan(pan:))))
panningView.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(didPan(pan:))))
addSubview(panningView)
layer.addSublayer(lineLayer)
layer.addSublayer(shapeLayer)
}
Expand All @@ -82,6 +87,7 @@ public class MIDITimeTablePlayheadView: UIView {

public override func layoutSubviews() {
super.layoutSubviews()
panningView.frame = CGRect(x: -panningOffset, y: -panningOffset, width: frame.size.width + (panningOffset * 2), height: frame.size.height + (panningOffset * 2))
lineLayer.frame.size = CGSize(width: lineWidth, height: lineHeight + (frame.height / 2))
lineLayer.frame.origin.y = frame.height - (frame.height / 2)
lineLayer.position.x = frame.width / 2
Expand All @@ -91,6 +97,21 @@ public class MIDITimeTablePlayheadView: UIView {
drawShapeLayer()
}

public override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
if clipsToBounds || isHidden || alpha == 0 {
return nil
}

for subview in subviews.reversed() {
let subPoint = subview.convert(point, from: self)
if let result = subview.hitTest(subPoint, with: event) {
return result
}
}

return nil
}

private func updatePosition() {
frame = CGRect(
x: rowHeaderWidth + (CGFloat(position) * measureBeatWidth) - (frame.size.width / 2),
Expand Down
6 changes: 3 additions & 3 deletions MIDITimeTableView/Source/MIDITimeTableView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ open class MIDITimeTableView: UIScrollView, MIDITimeTableCellViewDelegate, MIDIT
rangeheadView.lineHeight = contentSize.height - measureHeight
rangeheadView.measureBeatWidth = measureWidth / CGFloat(measureView.beatCount)
rangeheadView.isHidden = !showsPlayhead
bringSubview(toFront: rangeheadView)

// Grid layer
gridLayer.rowCount = rowHeaderCellViews.count
Expand Down Expand Up @@ -727,8 +728,10 @@ open class MIDITimeTableView: UIScrollView, MIDITimeTableCellViewDelegate, MIDIT
// Horizontal move
if translation.x > subbeatWidth, playheadView.frame.maxX < contentSize.width {
playheadView.position += 0.25
panGestureRecognizer.setTranslation(CGPoint(x: 0, y: translation.y), in: self)
} else if translation.x < -subbeatWidth, playheadView.frame.minX > headerCellWidth {
playheadView.position -= 0.25
panGestureRecognizer.setTranslation(CGPoint(x: 0, y: translation.y), in: self)
}

// Fire delegate
Expand All @@ -739,9 +742,6 @@ open class MIDITimeTableView: UIScrollView, MIDITimeTableCellViewDelegate, MIDIT
timeTableDelegate?.midiTimeTableView(self, didUpdateRangeHead: rangeheadView.position)
}
}

// Reset translation
panGestureRecognizer.setTranslation(.zero, in: self)
}

// MARK: MIDITimeTableHistoryDelegate
Expand Down

0 comments on commit 9bebeba

Please sign in to comment.