From 4b8eb19e22b08777f90da8b56eb22c8a51634903 Mon Sep 17 00:00:00 2001 From: amisha Date: Fri, 5 Aug 2022 11:58:05 +0530 Subject: [PATCH 1/3] Make end and start time setter --- .../Trimmer/PryntTrimmerView.swift | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/Sources/PryntTrimmerView/Trimmer/PryntTrimmerView.swift b/Sources/PryntTrimmerView/Trimmer/PryntTrimmerView.swift index c929446..b95030b 100644 --- a/Sources/PryntTrimmerView/Trimmer/PryntTrimmerView.swift +++ b/Sources/PryntTrimmerView/Trimmer/PryntTrimmerView.swift @@ -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() } } @@ -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 @@ -122,7 +121,6 @@ public protocol TrimmerViewDelegate: AnyObject { } private func setupHandleView() { - leftHandleView.isUserInteractionEnabled = true leftHandleView.layer.cornerRadius = 2.0 leftHandleView.translatesAutoresizingMaskIntoConstraints = false @@ -161,7 +159,6 @@ public protocol TrimmerViewDelegate: AnyObject { } private func setupMaskView() { - leftMaskView.isUserInteractionEnabled = false leftMaskView.backgroundColor = .white leftMaskView.alpha = 0.7 @@ -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) @@ -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)) @@ -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() @@ -298,14 +291,22 @@ public protocol TrimmerViewDelegate: AnyObject { /// The selected start time for the current asset. public var startTime: CMTime? { - let startPosition = leftHandleView.frame.origin.x + assetPreview.contentOffset.x - return getTime(from: startPosition) + get { + let startPosition = leftHandleView.frame.origin.x + assetPreview.contentOffset.x + return getTime(from: startPosition) + } set { + self.startTime = newValue + } } /// The selected end time for the current asset. public var endTime: CMTime? { - let endPosition = rightHandleView.frame.origin.x + assetPreview.contentOffset.x - handleWidth - return getTime(from: endPosition) + get { + let endPosition = rightHandleView.frame.origin.x + assetPreview.contentOffset.x - handleWidth + return getTime(from: endPosition) + } set { + self.endTime = newValue + } } private func updateSelectedTime(stoppedMoving: Bool) { @@ -330,7 +331,6 @@ public protocol TrimmerViewDelegate: AnyObject { } // MARK: - Scroll View Delegate - public func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) { updateSelectedTime(stoppedMoving: true) } @@ -340,6 +340,7 @@ public protocol TrimmerViewDelegate: AnyObject { updateSelectedTime(stoppedMoving: true) } } + public func scrollViewDidScroll(_ scrollView: UIScrollView) { updateSelectedTime(stoppedMoving: false) } From 352f2dcd983c619038b0263fe370e360b5f93773 Mon Sep 17 00:00:00 2001 From: amisha Date: Mon, 8 Aug 2022 17:44:14 +0530 Subject: [PATCH 2/3] Fix left side edge --- .../PryntTrimmerView/Trimmer/PryntTrimmerView.swift | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/Sources/PryntTrimmerView/Trimmer/PryntTrimmerView.swift b/Sources/PryntTrimmerView/Trimmer/PryntTrimmerView.swift index b95030b..560f77e 100644 --- a/Sources/PryntTrimmerView/Trimmer/PryntTrimmerView.swift +++ b/Sources/PryntTrimmerView/Trimmer/PryntTrimmerView.swift @@ -260,6 +260,8 @@ public protocol TrimmerViewDelegate: AnyObject { private func updateRightConstraint(with translation: CGPoint) { let maxConstraint = min(2 * handleWidth - frame.width + leftHandleView.frame.origin.x + minimumDistanceBetweenHandle, 0) let newConstraint = max(min(0, currentRightConstraint + translation.x), maxConstraint) + print("XX MAX CONSTRAINT :: \(maxConstraint)") + print("XX NEW CONSTRAINT :: \(newConstraint)") rightConstraint?.constant = newConstraint } @@ -295,7 +297,10 @@ public protocol TrimmerViewDelegate: AnyObject { let startPosition = leftHandleView.frame.origin.x + assetPreview.contentOffset.x return getTime(from: startPosition) } set { - self.startTime = newValue + if let value = newValue, let position = getPosition(from: value) { + let newPosition = position - assetPreview.contentOffset.x + updateLeftConstraint(with: CGPoint(x: newPosition, y: 0)) + } } } @@ -305,7 +310,11 @@ public protocol TrimmerViewDelegate: AnyObject { let endPosition = rightHandleView.frame.origin.x + assetPreview.contentOffset.x - handleWidth return getTime(from: endPosition) } set { - self.endTime = newValue + if let value = newValue, let position = getPosition(from: value) { + let newPosition = position - assetPreview.contentOffset.x + handleWidth + print("XX -- NEW Value :: \(position) -- \(newPosition)") + updateRightConstraint(with: CGPoint(x: newPosition, y: 0)) + } } } From 6744fa5433072dacbf01f37ec39efdce5501c0b4 Mon Sep 17 00:00:00 2001 From: amisha Date: Mon, 8 Aug 2022 18:01:42 +0530 Subject: [PATCH 3/3] Add 2 separate methods --- .../Trimmer/PryntTrimmerView.swift | 39 +++++++++---------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/Sources/PryntTrimmerView/Trimmer/PryntTrimmerView.swift b/Sources/PryntTrimmerView/Trimmer/PryntTrimmerView.swift index 560f77e..d5e1333 100644 --- a/Sources/PryntTrimmerView/Trimmer/PryntTrimmerView.swift +++ b/Sources/PryntTrimmerView/Trimmer/PryntTrimmerView.swift @@ -260,8 +260,6 @@ public protocol TrimmerViewDelegate: AnyObject { private func updateRightConstraint(with translation: CGPoint) { let maxConstraint = min(2 * handleWidth - frame.width + leftHandleView.frame.origin.x + minimumDistanceBetweenHandle, 0) let newConstraint = max(min(0, currentRightConstraint + translation.x), maxConstraint) - print("XX MAX CONSTRAINT :: \(maxConstraint)") - print("XX NEW CONSTRAINT :: \(newConstraint)") rightConstraint?.constant = newConstraint } @@ -293,28 +291,29 @@ public protocol TrimmerViewDelegate: AnyObject { /// The selected start time for the current asset. public var startTime: CMTime? { - get { - let startPosition = leftHandleView.frame.origin.x + assetPreview.contentOffset.x - return getTime(from: startPosition) - } set { - if let value = newValue, let position = getPosition(from: value) { - let newPosition = position - assetPreview.contentOffset.x - updateLeftConstraint(with: CGPoint(x: newPosition, y: 0)) - } - } + let startPosition = leftHandleView.frame.origin.x + assetPreview.contentOffset.x + return getTime(from: startPosition) } /// The selected end time for the current asset. public var endTime: CMTime? { - get { - let endPosition = rightHandleView.frame.origin.x + assetPreview.contentOffset.x - handleWidth - return getTime(from: endPosition) - } set { - if let value = newValue, let position = getPosition(from: value) { - let newPosition = position - assetPreview.contentOffset.x + handleWidth - print("XX -- NEW Value :: \(position) -- \(newPosition)") - updateRightConstraint(with: CGPoint(x: newPosition, y: 0)) - } + let endPosition = rightHandleView.frame.origin.x + assetPreview.contentOffset.x - handleWidth + 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) } }