From 668b20f77b8942f76a29f3a0ca06b02fc4fe00f7 Mon Sep 17 00:00:00 2001 From: Vasavi Kanamarlapudi Date: Tue, 2 Jul 2024 13:22:39 +0530 Subject: [PATCH] Digital ACT-191 ONEAPP-7013 story: Updating scrollbar position on scrolling carousel content --- VDS/Components/Carousel/Carousel.swift | 57 ++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 4 deletions(-) diff --git a/VDS/Components/Carousel/Carousel.swift b/VDS/Components/Carousel/Carousel.swift index 0347efc0..498e1dc5 100644 --- a/VDS/Components/Carousel/Carousel.swift +++ b/VDS/Components/Carousel/Carousel.swift @@ -365,7 +365,6 @@ open class Carousel: View { } updatePaginationControls() - getSlotWidth() addCarouselSlots() } @@ -386,6 +385,7 @@ open class Carousel: View { // MARK: - Private Methods //-------------------------------------------------- private func addCarouselSlots() { + getSlotWidth() if containerView.frame.size.width > 0 { containerViewHeightConstraint?.isActive = false containerStackHeightConstraint?.isActive = false @@ -405,6 +405,7 @@ open class Carousel: View { $0.backgroundColor = UIColor(red: CGFloat(216) / 255.0, green: CGFloat(218) / 255.0, blue: CGFloat(218) / 255.0, alpha: 1) } scrollView.addSubview(carouselSlot) + scrollView.delegate = self let size = ratioSize(for: minimumSlotWidth) slotHeight = size.height carouselSlot @@ -553,15 +554,49 @@ open class Carousel: View { if carouselScrollBar.numberOfSlides > 0 { let scrollContentSizeWidth = scrollView.contentSize.width let totalPositions = totalPositions() - let multiplier: Float = (position == 1) ? 0 : Float((position)-1) / Float(totalPositions) - let xPos = (position == totalPositions) ? (scrollContentSizeWidth - containerView.frame.size.width) : CGFloat(Float(scrollContentSizeWidth) * multiplier) - carouselScrollBar.scrubberId = position + var xPos = 0.0 + if position == 1 { + xPos = 0.0 + } else if position == totalPositions { + xPos = scrollContentSizeWidth - containerView.frame.size.width + } else { + let isScrollbarSuppressed = data.count > 0 && layout.value == data.count + let isPeekMinimumOnTablet = UIDevice.isIPad && peek == .minimum + if !isScrollbarSuppressed { + let subpart = minimumSlotWidth + gutter.value + let xPosition = CGFloat( Float(position-1) * Float(layout.value) * Float(subpart)) + switch peek { + case .standard: + if UIDevice.isIPad { + xPos = xPosition - (minimumSlotWidth/(CGFloat(layout.value) + 3))/2 + } else if layout == .oneUP { + xPos = xPosition - gutter.value - (minimumSlotWidth/4)/2 + } + case .minimum: + xPos = isPeekMinimumOnTablet ? xPosition : xPosition - peekMinimum/2 + case .none: + xPos = xPosition + } + } + } + carouselScrollBar.scrubberId = position+1 let yPos = scrollView.contentOffset.y scrollView.setContentOffset(CGPoint(x: xPos, y: yPos), animated: true) showPaginationControls() } } + func updateScrollbarPosition(targetContentOffsetXPos:CGFloat) { + let scrollContentSizeWidth = scrollView.contentSize.width + let totalPositions = totalPositions() + let layoutSpace = Int (floor( Double(scrollContentSizeWidth / Double(totalPositions)))) + let remindSpace = Int(targetContentOffsetXPos) % layoutSpace + var contentPos = (Int(targetContentOffsetXPos) / layoutSpace) + 1 + contentPos = remindSpace > layoutSpace/2 ? contentPos+1 : contentPos + carouselScrollBar.position = contentPos + updateScrollPosition(position: contentPos, callbackText: "ScrollViewMoved") + } + private func totalPositions() -> Int { return Int (ceil (Double(carouselScrollBar.numberOfSlides) / Double(_layout.value))) } @@ -576,3 +611,17 @@ open class Carousel: View { } } } + +extension Carousel: UIScrollViewDelegate { + //-------------------------------------------------- + // MARK: - UIScrollView Delegate + //-------------------------------------------------- + public func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer) { + updateScrollbarPosition(targetContentOffsetXPos: targetContentOffset.pointee.x) + } + public func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) { + var visibleRect = CGRect() + visibleRect.origin = scrollView.contentOffset + updateScrollbarPosition(targetContentOffsetXPos: visibleRect.origin.x) + } +}