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

Make end and start time setter #88

Open
wants to merge 3 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
29 changes: 19 additions & 10 deletions Sources/PryntTrimmerView/Trimmer/PryntTrimmerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public protocol TrimmerViewDelegate: AnyObject {
/// The color of the handles on the side of the view
@IBInspectable public var handleColor: UIColor = UIColor.gray {
didSet {
updateHandleColor()
updateHandleColor()
}
}

Expand Down Expand Up @@ -83,7 +83,6 @@ public protocol TrimmerViewDelegate: AnyObject {
public var minDuration: Double = 3

// MARK: - View & constraints configurations

override func setupSubviews() {
super.setupSubviews()
layer.cornerRadius = 2
Expand Down Expand Up @@ -122,7 +121,6 @@ public protocol TrimmerViewDelegate: AnyObject {
}

private func setupHandleView() {

leftHandleView.isUserInteractionEnabled = true
leftHandleView.layer.cornerRadius = 2.0
leftHandleView.translatesAutoresizingMaskIntoConstraints = false
Expand Down Expand Up @@ -161,7 +159,6 @@ public protocol TrimmerViewDelegate: AnyObject {
}

private func setupMaskView() {

leftMaskView.isUserInteractionEnabled = false
leftMaskView.backgroundColor = .white
leftMaskView.alpha = 0.7
Expand All @@ -186,7 +183,6 @@ public protocol TrimmerViewDelegate: AnyObject {
}

private func setupPositionBar() {

positionBar.frame = CGRect(x: 0, y: 0, width: 3, height: frame.height)
positionBar.backgroundColor = positionBarColor
positionBar.center = CGPoint(x: leftHandleView.frame.maxX, y: center.y)
Expand All @@ -203,7 +199,6 @@ public protocol TrimmerViewDelegate: AnyObject {
}

private func setupGestures() {

let leftPanGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(TrimmerView.handlePanGesture))
leftHandleView.addGestureRecognizer(leftPanGestureRecognizer)
let rightPanGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(TrimmerView.handlePanGesture))
Expand Down Expand Up @@ -286,10 +281,8 @@ public protocol TrimmerViewDelegate: AnyObject {
/// Move the position bar to the given time.
public func seek(to time: CMTime) {
if let newPosition = getPosition(from: time) {

let offsetPosition = newPosition - assetPreview.contentOffset.x - leftHandleView.frame.origin.x
let maxPosition = rightHandleView.frame.origin.x - (leftHandleView.frame.origin.x + handleWidth)
- positionBar.frame.width
let maxPosition = rightHandleView.frame.origin.x - (leftHandleView.frame.origin.x + handleWidth) - positionBar.frame.width
let normalizedPosition = min(max(0, offsetPosition), maxPosition)
positionConstraint?.constant = normalizedPosition
layoutIfNeeded()
Expand All @@ -308,6 +301,22 @@ public protocol TrimmerViewDelegate: AnyObject {
return getTime(from: endPosition)
}

/// Move the left trimmer handle to the given time.
public func moveLeftHandle(to time: CMTime) {
if let newPosition = getPosition(from: time) {
updateLeftConstraint(with: CGPoint(x: newPosition - currentLeftConstraint, y: 0))
updateSelectedTime(stoppedMoving: false)
}
}

/// Move the right trimmer handle to the given time.
public func moveRightHandle(to time: CMTime) {
if let newPosition = getPosition(from: time) {
updateRightConstraint(with: CGPoint(x: newPosition - frame.width - currentRightConstraint + 2 * handleWidth, y: 0))
updateSelectedTime(stoppedMoving: false)
}
}

private func updateSelectedTime(stoppedMoving: Bool) {
guard let playerTime = positionBarTime else {
return
Expand All @@ -330,7 +339,6 @@ public protocol TrimmerViewDelegate: AnyObject {
}

// MARK: - Scroll View Delegate

public func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
updateSelectedTime(stoppedMoving: true)
}
Expand All @@ -340,6 +348,7 @@ public protocol TrimmerViewDelegate: AnyObject {
updateSelectedTime(stoppedMoving: true)
}
}

public func scrollViewDidScroll(_ scrollView: UIScrollView) {
updateSelectedTime(stoppedMoving: false)
}
Expand Down