Digital ACT-191 ONEAPP-6830 story: Interaction type: Drag scrollbar thumb

This commit is contained in:
vasavk 2024-03-21 10:58:53 +05:30
parent f7ef52251b
commit 51a00f9b68

View File

@ -44,7 +44,7 @@ open class CarouselScrollbar: View {
_numberOfSlides = 1
}
setThumbWidth()
setThumb(position)
scrollThumbToPosition(position)
setNeedsUpdate()
}
}
@ -59,7 +59,7 @@ open class CarouselScrollbar: View {
_selectedLayout = .oneUP
}
setThumbWidth()
setThumb(position)
scrollThumbToPosition(position)
setNeedsUpdate()
}
}
@ -104,7 +104,7 @@ open class CarouselScrollbar: View {
} else {
_position = 1
}
setThumb(position)
scrollThumbToPosition(position)
setNeedsUpdate()
}
}
@ -123,6 +123,7 @@ open class CarouselScrollbar: View {
internal var heightConstraint: NSLayoutConstraint?
internal var totalPositions: Int? = 1
internal var _position: Int? = 1
internal var trayOriginalCenter: CGPoint!
private let trackViewWidth = 96
private let trackViewHeight: CGFloat = 4
@ -212,7 +213,7 @@ open class CarouselScrollbar: View {
//Thumbview
thumbView.frame = CGRectMake(20, 20, CGFloat(thumbWidth), trackViewHeight)
thumbView.layer.cornerRadius = cornerRadius
thumbView.addGestureRecognizer(UIPanGestureRecognizer(target: self, action:(#selector(self.handleGesture(_:)))))
thumbView.addGestureRecognizer(UIPanGestureRecognizer(target: self, action:(#selector(self.onScrubberDrag(_:)))))
addSubview(thumbView)
updateActiveOverlays()
}
@ -249,23 +250,17 @@ open class CarouselScrollbar: View {
updateActiveOverlays()
}
@objc func handleGesture(_ sender: UIPanGestureRecognizer) {
switch sender.state {
case .began:
print("began")
break
case .changed:
print("changed")
break
case .cancelled:
print("cancelled")
break
case .ended:
print("ended")
break
default:
break
@objc func onScrubberDrag(_ sender: UIPanGestureRecognizer) {
let translation = sender.translation(in: thumbView)
if sender.state == UIGestureRecognizer.State.began {
trayOriginalCenter = thumbView.center
} else if sender.state == UIGestureRecognizer.State.changed {
let movePositions = Int (ceil (Double(translation.x) / Double(actualThumbWidth)))
setThumb(at: (position ?? 1) + movePositions)
}
else if sender.state == UIGestureRecognizer.State.cancelled || sender.state == UIGestureRecognizer.State.ended {
let movePositions = Int (ceil (Double(translation.x) / Double(actualThumbWidth)))
position = (position ?? 1) + movePositions
}
}
@ -332,8 +327,14 @@ open class CarouselScrollbar: View {
private func checkPositions() {
totalPositions = Int (ceil (Double(numberOfSlides ?? 1) / Double(_selectedLayout.value)))
}
private func scrollThumbToPosition(_ position: Int?) {
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(100)) { [weak self] in
self?.setThumb(at: position)
}
}
private func setThumb(_ position: Int?) {
private func setThumb(at position: Int?) {
thumbView.frame.origin.x = CGFloat(Float((position ?? 1) - 1) * actualThumbWidth) + trackView.frame.origin.x
updateActiveOverlays()
}