Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented UIPanGestureRecognizer for positionBar #65

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions Sources/PryntTrimmerView/Trimmer/PryntTrimmerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public protocol TrimmerViewDelegate: class {
positionBar.center = CGPoint(x: leftHandleView.frame.maxX, y: center.y)
positionBar.layer.cornerRadius = 1
positionBar.translatesAutoresizingMaskIntoConstraints = false
positionBar.isUserInteractionEnabled = false
positionBar.isUserInteractionEnabled = true
addSubview(positionBar)

positionBar.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
Expand All @@ -208,6 +208,8 @@ public protocol TrimmerViewDelegate: class {
leftHandleView.addGestureRecognizer(leftPanGestureRecognizer)
let rightPanGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(TrimmerView.handlePanGesture))
rightHandleView.addGestureRecognizer(rightPanGestureRecognizer)
let positionBarPanGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(TrimmerView.handlePanGesture))
positionBar.addGestureRecognizer(positionBarPanGestureRecognizer)
}

private func updateMainColor() {
Expand All @@ -225,6 +227,7 @@ public protocol TrimmerViewDelegate: class {

@objc func handlePanGesture(_ gestureRecognizer: UIPanGestureRecognizer) {
guard let view = gestureRecognizer.view, let superView = gestureRecognizer.view?.superview else { return }
let isPositionBarGesture = view == positionBar
let isLeftGesture = view == leftHandleView
switch gestureRecognizer.state {

Expand All @@ -237,13 +240,18 @@ public protocol TrimmerViewDelegate: class {
updateSelectedTime(stoppedMoving: false)
case .changed:
let translation = gestureRecognizer.translation(in: superView)
if isLeftGesture {
if isPositionBarGesture {
updatePositionBarConstraint(with: translation)
gestureRecognizer.setTranslation(.zero, in: superView)
} else if isLeftGesture {
updateLeftConstraint(with: translation)
} else {
updateRightConstraint(with: translation)
}
layoutIfNeeded()
if let startTime = startTime, isLeftGesture {
if isPositionBarGesture, let positionBarTime = positionBarTime {
seek(to: positionBarTime)
} else if let startTime = startTime, isLeftGesture {
seek(to: startTime)
} else if let endTime = endTime {
seek(to: endTime)
Expand All @@ -255,6 +263,15 @@ public protocol TrimmerViewDelegate: class {
default: break
}
}

public override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
let frame = positionBar.frame.insetBy(dx: -15, dy: 0)
if frame.contains(point) {
return positionBar
}

return super.hitTest(point, with: event)
}

private func updateLeftConstraint(with translation: CGPoint) {
let maxConstraint = max(rightHandleView.frame.origin.x - handleWidth - minimumDistanceBetweenHandle, 0)
Expand All @@ -267,6 +284,11 @@ public protocol TrimmerViewDelegate: class {
let newConstraint = max(min(0, currentRightConstraint + translation.x), maxConstraint)
rightConstraint?.constant = newConstraint
}

private func updatePositionBarConstraint(with translation: CGPoint) {
let newConstraint = positionConstraint!.constant + translation.x
positionConstraint?.constant = newConstraint
}

// MARK: - Asset loading

Expand Down