Digital ACT-191 ONEAPP-7013 story: Updating scrollbar position on scrolling carousel content
This commit is contained in:
parent
f13451ea37
commit
668b20f77b
@ -365,7 +365,6 @@ open class Carousel: View {
|
|||||||
}
|
}
|
||||||
|
|
||||||
updatePaginationControls()
|
updatePaginationControls()
|
||||||
getSlotWidth()
|
|
||||||
addCarouselSlots()
|
addCarouselSlots()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -386,6 +385,7 @@ open class Carousel: View {
|
|||||||
// MARK: - Private Methods
|
// MARK: - Private Methods
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
private func addCarouselSlots() {
|
private func addCarouselSlots() {
|
||||||
|
getSlotWidth()
|
||||||
if containerView.frame.size.width > 0 {
|
if containerView.frame.size.width > 0 {
|
||||||
containerViewHeightConstraint?.isActive = false
|
containerViewHeightConstraint?.isActive = false
|
||||||
containerStackHeightConstraint?.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)
|
$0.backgroundColor = UIColor(red: CGFloat(216) / 255.0, green: CGFloat(218) / 255.0, blue: CGFloat(218) / 255.0, alpha: 1)
|
||||||
}
|
}
|
||||||
scrollView.addSubview(carouselSlot)
|
scrollView.addSubview(carouselSlot)
|
||||||
|
scrollView.delegate = self
|
||||||
let size = ratioSize(for: minimumSlotWidth)
|
let size = ratioSize(for: minimumSlotWidth)
|
||||||
slotHeight = size.height
|
slotHeight = size.height
|
||||||
carouselSlot
|
carouselSlot
|
||||||
@ -553,15 +554,49 @@ open class Carousel: View {
|
|||||||
if carouselScrollBar.numberOfSlides > 0 {
|
if carouselScrollBar.numberOfSlides > 0 {
|
||||||
let scrollContentSizeWidth = scrollView.contentSize.width
|
let scrollContentSizeWidth = scrollView.contentSize.width
|
||||||
let totalPositions = totalPositions()
|
let totalPositions = totalPositions()
|
||||||
let multiplier: Float = (position == 1) ? 0 : Float((position)-1) / Float(totalPositions)
|
var xPos = 0.0
|
||||||
let xPos = (position == totalPositions) ? (scrollContentSizeWidth - containerView.frame.size.width) : CGFloat(Float(scrollContentSizeWidth) * multiplier)
|
if position == 1 {
|
||||||
carouselScrollBar.scrubberId = position
|
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
|
let yPos = scrollView.contentOffset.y
|
||||||
scrollView.setContentOffset(CGPoint(x: xPos, y: yPos), animated: true)
|
scrollView.setContentOffset(CGPoint(x: xPos, y: yPos), animated: true)
|
||||||
showPaginationControls()
|
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 {
|
private func totalPositions() -> Int {
|
||||||
return Int (ceil (Double(carouselScrollBar.numberOfSlides) / Double(_layout.value)))
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user