From 6380026eaa7fa4f6197bba425dbb3f8d2570e9eb Mon Sep 17 00:00:00 2001 From: vasavk Date: Wed, 27 Mar 2024 16:01:15 +0530 Subject: [PATCH] Digital ACT-191 ONEAPP-6830 story: updated with required callbacks --- .../CarouselScrollbar/CarouselScrollbar.swift | 42 ++++++++++++++----- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/VDS/Components/CarouselScrollbar/CarouselScrollbar.swift b/VDS/Components/CarouselScrollbar/CarouselScrollbar.swift index 6a072ccd..260f14e5 100644 --- a/VDS/Components/CarouselScrollbar/CarouselScrollbar.swift +++ b/VDS/Components/CarouselScrollbar/CarouselScrollbar.swift @@ -105,7 +105,20 @@ open class CarouselScrollbar: View { open var scrubberId: Int? { didSet { setNeedsUpdate() } } /// A callback when the scrubber position changes. Passes parameters (position). - open var onScrubberDidChange: ((Int) -> Void)? + open var onScrubberDrag: ((Int) -> Void)? + + /// A callback when the thumb move forward. Passes parameters (position). + open var onMoveForward: ((Int) -> Void)? + + /// A callback when the thumb move backward. Passes parameters (position). + open var onMoveBackward: ((Int) -> Void)? + + /// A callback when the thumb touch start. Passes parameters (position). + open var onThumbTouchStart: ((Int) -> Void)? + + /// A callback when the thumb touch end. Passes parameters (position). + open var onThumbTouchEnd: ((Int) -> Void)? + //-------------------------------------------------- // MARK: - Private Properties @@ -154,6 +167,7 @@ open class CarouselScrollbar: View { open override func setup() { super.setup() + isAccessibilityElement = true accessibilityLabel = "Carousel Scrollbar" addSubview(containerView) @@ -201,7 +215,7 @@ open class CarouselScrollbar: View { //Thumbview thumbView.frame = CGRectMake(0, 0, CGFloat(thumbWidth), containerSize.height) thumbView.backgroundColor = .clear - thumbView.addGestureRecognizer(UIPanGestureRecognizer(target: self, action:(#selector(onScrubberDrag(_:))))) + thumbView.addGestureRecognizer(UIPanGestureRecognizer(target: self, action:(#selector(onScrubberChange(_:))))) containerView.addSubview(thumbView) updateActiveOverlays() @@ -233,14 +247,16 @@ open class CarouselScrollbar: View { //-------------------------------------------------- // MARK: - Private Methods //-------------------------------------------------- - func onMoveBackward() { + func movePositionBackward() { position = position - 1 scrollThumbToPosition(position) + self.onMoveBackward?(position) } - func onMoveForward() { + func movePositionForward() { position = position + 1 scrollThumbToPosition(position) + self.onMoveForward?(position) } // Compute track width and should maintain minimum thumb width if needed @@ -281,14 +297,14 @@ open class CarouselScrollbar: View { totalPositions = Int (ceil (Double(numberOfSlides) / Double(_selectedLayout.value))) } - private func scrollThumbToPosition(_ position: Int?) { + private func scrollThumbToPosition(_ position: Int) { self.setThumb(at: position) - self.onScrubberDidChange?(position ?? 1) + self.onScrubberDrag?(position) } - private func setThumb(at position: Int?) { + private func setThumb(at position: Int) { UIView.animate(withDuration: 0.1, delay: 0.0, options: .curveLinear, animations: { - self.thumbView.frame.origin.x = CGFloat(Float((position ?? 1) - 1) * self.computedWidth) + self.trackView.frame.origin.x + self.thumbView.frame.origin.x = CGFloat(Float((position) - 1) * self.computedWidth) + self.trackView.frame.origin.x self.updateActiveOverlays() }) } @@ -298,10 +314,11 @@ open class CarouselScrollbar: View { //-------------------------------------------------- // Drag scrollbar thumb to move it to the left or right. // Upon releases of drag, the scrollbar thumb snaps to the closest full position and the scrollbar returns to original size without delay. - @objc func onScrubberDrag(_ sender: UIPanGestureRecognizer) { + @objc func onScrubberChange(_ sender: UIPanGestureRecognizer) { let translation = sender.translation(in: thumbView) if sender.state == UIGestureRecognizer.State.began { trayOriginalCenter = thumbView.center + self.onThumbTouchStart?(position) } else if sender.state == UIGestureRecognizer.State.changed { let draggedPositions = Int (ceil (Double(translation.x) / Double(computedWidth))) setThumb(at: position + draggedPositions) @@ -309,17 +326,20 @@ open class CarouselScrollbar: View { else if sender.state == UIGestureRecognizer.State.cancelled || sender.state == UIGestureRecognizer.State.ended { let draggedPositions = Int (ceil (Double(translation.x) / Double(computedWidth))) position = ((position + draggedPositions) < 1) ? 1 : (position + draggedPositions) + if sender.state == UIGestureRecognizer.State.ended { + self.onThumbTouchEnd?(position) + } } } // Move the scrollbar thumb to the left while tapping on the left side of the scrubber. @objc func onLeftViewLongPressRecognizer(_ gesture: UILongPressGestureRecognizer) { - animateOverlay(layer: leftActiveOverlayLayer, with: gesture, onGestureEnd: onMoveBackward) + animateOverlay(layer: leftActiveOverlayLayer, with: gesture, onGestureEnd: movePositionBackward) } // Move the scrollbar thumb to the right while tapping on the right side of the scrubber. @objc func onRightViewLongPressRecognizer(_ gesture: UILongPressGestureRecognizer) { - animateOverlay(layer: rightActiveOverlayLayer, with: gesture, onGestureEnd: onMoveForward) + animateOverlay(layer: rightActiveOverlayLayer, with: gesture, onGestureEnd: movePositionForward) } private func animateOverlay(layer: CALayer, with gesture: UILongPressGestureRecognizer, onGestureEnd: @escaping(() -> Void)) {