Digital ACT-191 ONEAPP-6830 story: updated with required callbacks
This commit is contained in:
parent
4a9262c0dc
commit
6380026eaa
@ -105,7 +105,20 @@ open class CarouselScrollbar: View {
|
|||||||
open var scrubberId: Int? { didSet { setNeedsUpdate() } }
|
open var scrubberId: Int? { didSet { setNeedsUpdate() } }
|
||||||
|
|
||||||
/// A callback when the scrubber position changes. Passes parameters (position).
|
/// 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
|
// MARK: - Private Properties
|
||||||
@ -154,6 +167,7 @@ open class CarouselScrollbar: View {
|
|||||||
|
|
||||||
open override func setup() {
|
open override func setup() {
|
||||||
super.setup()
|
super.setup()
|
||||||
|
isAccessibilityElement = true
|
||||||
accessibilityLabel = "Carousel Scrollbar"
|
accessibilityLabel = "Carousel Scrollbar"
|
||||||
|
|
||||||
addSubview(containerView)
|
addSubview(containerView)
|
||||||
@ -201,7 +215,7 @@ open class CarouselScrollbar: View {
|
|||||||
//Thumbview
|
//Thumbview
|
||||||
thumbView.frame = CGRectMake(0, 0, CGFloat(thumbWidth), containerSize.height)
|
thumbView.frame = CGRectMake(0, 0, CGFloat(thumbWidth), containerSize.height)
|
||||||
thumbView.backgroundColor = .clear
|
thumbView.backgroundColor = .clear
|
||||||
thumbView.addGestureRecognizer(UIPanGestureRecognizer(target: self, action:(#selector(onScrubberDrag(_:)))))
|
thumbView.addGestureRecognizer(UIPanGestureRecognizer(target: self, action:(#selector(onScrubberChange(_:)))))
|
||||||
containerView.addSubview(thumbView)
|
containerView.addSubview(thumbView)
|
||||||
updateActiveOverlays()
|
updateActiveOverlays()
|
||||||
|
|
||||||
@ -233,14 +247,16 @@ open class CarouselScrollbar: View {
|
|||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - Private Methods
|
// MARK: - Private Methods
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
func onMoveBackward() {
|
func movePositionBackward() {
|
||||||
position = position - 1
|
position = position - 1
|
||||||
scrollThumbToPosition(position)
|
scrollThumbToPosition(position)
|
||||||
|
self.onMoveBackward?(position)
|
||||||
}
|
}
|
||||||
|
|
||||||
func onMoveForward() {
|
func movePositionForward() {
|
||||||
position = position + 1
|
position = position + 1
|
||||||
scrollThumbToPosition(position)
|
scrollThumbToPosition(position)
|
||||||
|
self.onMoveForward?(position)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compute track width and should maintain minimum thumb width if needed
|
// 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)))
|
totalPositions = Int (ceil (Double(numberOfSlides) / Double(_selectedLayout.value)))
|
||||||
}
|
}
|
||||||
|
|
||||||
private func scrollThumbToPosition(_ position: Int?) {
|
private func scrollThumbToPosition(_ position: Int) {
|
||||||
self.setThumb(at: position)
|
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: {
|
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()
|
self.updateActiveOverlays()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -298,10 +314,11 @@ open class CarouselScrollbar: View {
|
|||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// Drag scrollbar thumb to move it to the left or right.
|
// 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.
|
// 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)
|
let translation = sender.translation(in: thumbView)
|
||||||
if sender.state == UIGestureRecognizer.State.began {
|
if sender.state == UIGestureRecognizer.State.began {
|
||||||
trayOriginalCenter = thumbView.center
|
trayOriginalCenter = thumbView.center
|
||||||
|
self.onThumbTouchStart?(position)
|
||||||
} else if sender.state == UIGestureRecognizer.State.changed {
|
} else if sender.state == UIGestureRecognizer.State.changed {
|
||||||
let draggedPositions = Int (ceil (Double(translation.x) / Double(computedWidth)))
|
let draggedPositions = Int (ceil (Double(translation.x) / Double(computedWidth)))
|
||||||
setThumb(at: position + draggedPositions)
|
setThumb(at: position + draggedPositions)
|
||||||
@ -309,17 +326,20 @@ open class CarouselScrollbar: View {
|
|||||||
else if sender.state == UIGestureRecognizer.State.cancelled || sender.state == UIGestureRecognizer.State.ended {
|
else if sender.state == UIGestureRecognizer.State.cancelled || sender.state == UIGestureRecognizer.State.ended {
|
||||||
let draggedPositions = Int (ceil (Double(translation.x) / Double(computedWidth)))
|
let draggedPositions = Int (ceil (Double(translation.x) / Double(computedWidth)))
|
||||||
position = ((position + draggedPositions) < 1) ? 1 : (position + draggedPositions)
|
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.
|
// Move the scrollbar thumb to the left while tapping on the left side of the scrubber.
|
||||||
@objc func onLeftViewLongPressRecognizer(_ gesture: UILongPressGestureRecognizer) {
|
@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.
|
// Move the scrollbar thumb to the right while tapping on the right side of the scrubber.
|
||||||
@objc func onRightViewLongPressRecognizer(_ gesture: UILongPressGestureRecognizer) {
|
@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)) {
|
private func animateOverlay(layer: CALayer, with gesture: UILongPressGestureRecognizer, onGestureEnd: @escaping(() -> Void)) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user