Digital ACT-191 ONEAPP-7013 story: Updating scrollbar position on scrolling carousel content

This commit is contained in:
Vasavi Kanamarlapudi 2024-07-02 13:22:39 +05:30
parent f13451ea37
commit 668b20f77b

View File

@ -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<CGPoint>) {
updateScrollbarPosition(targetContentOffsetXPos: targetContentOffset.pointee.x)
}
public func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
var visibleRect = CGRect()
visibleRect.origin = scrollView.contentOffset
updateScrollbarPosition(targetContentOffsetXPos: visibleRect.origin.x)
}
}