added publishers/cancellables
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
parent
6380026eaa
commit
c64caa7a9f
@ -100,25 +100,99 @@ open class CarouselScrollbar: View {
|
|||||||
setNeedsUpdate()
|
setNeedsUpdate()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Allows a unique id to be passed into the thumb and track of the thumb(scrubber).
|
/// Allows a unique id to be passed into the thumb and track of the thumb(scrubber).
|
||||||
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 onScrubberDrag: ((Int) -> Void)?
|
open var onScrubberDrag: ((Int) -> Void)? {
|
||||||
|
get { nil }
|
||||||
|
set {
|
||||||
|
onScrubberDragCancellable?.cancel()
|
||||||
|
if let newValue {
|
||||||
|
onScrubberDragCancellable = onScrubberDragPublisher
|
||||||
|
.sink { c in
|
||||||
|
newValue(c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A publisher for when the scrubber position changes. Passes parameters (position).
|
||||||
|
open var onScrubberDragPublisher = PassthroughSubject<Int, Never>()
|
||||||
|
private var onScrubberDragCancellable: AnyCancellable?
|
||||||
|
|
||||||
/// A callback when the thumb move forward. Passes parameters (position).
|
/// A callback when the thumb move forward. Passes parameters (position).
|
||||||
open var onMoveForward: ((Int) -> Void)?
|
open var onMoveForward: ((Int) -> Void)? {
|
||||||
|
get { nil }
|
||||||
|
set {
|
||||||
|
onMoveForwardCancellable?.cancel()
|
||||||
|
if let newValue {
|
||||||
|
onMoveForwardCancellable = onMoveForwardPublisher
|
||||||
|
.sink { c in
|
||||||
|
newValue(c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A publisher for when the thumb move forward. Passes parameters (position).
|
||||||
|
open var onMoveForwardPublisher = PassthroughSubject<Int, Never>()
|
||||||
|
private var onMoveForwardCancellable: AnyCancellable?
|
||||||
|
|
||||||
/// A callback when the thumb move backward. Passes parameters (position).
|
/// A callback when the thumb move backward. Passes parameters (position).
|
||||||
open var onMoveBackward: ((Int) -> Void)?
|
open var onMoveBackward: ((Int) -> Void)? {
|
||||||
|
get { nil }
|
||||||
|
set {
|
||||||
|
onMoveBackwardCancellable?.cancel()
|
||||||
|
if let newValue {
|
||||||
|
onMoveBackwardCancellable = onMoveBackwardPublisher
|
||||||
|
.sink { c in
|
||||||
|
newValue(c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A publisher for when the thumb move backward. Passes parameters (position).
|
||||||
|
open var onMoveBackwardPublisher = PassthroughSubject<Int, Never>()
|
||||||
|
private var onMoveBackwardCancellable: AnyCancellable?
|
||||||
|
|
||||||
/// A callback when the thumb touch start. Passes parameters (position).
|
/// A callback when the thumb touch start. Passes parameters (position).
|
||||||
open var onThumbTouchStart: ((Int) -> Void)?
|
open var onThumbTouchStart: ((Int) -> Void)? {
|
||||||
|
get { nil }
|
||||||
|
set {
|
||||||
|
onThumbTouchStartCancellable?.cancel()
|
||||||
|
if let newValue {
|
||||||
|
onThumbTouchStartCancellable = onThumbTouchStartPublisher
|
||||||
|
.sink { c in
|
||||||
|
newValue(c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A publisher for when the thumb touch start. Passes parameters (position).
|
||||||
|
open var onThumbTouchStartPublisher = PassthroughSubject<Int, Never>()
|
||||||
|
private var onThumbTouchStartCancellable: AnyCancellable?
|
||||||
|
|
||||||
/// A callback when the thumb touch end. Passes parameters (position).
|
/// A callback when the thumb touch end. Passes parameters (position).
|
||||||
open var onThumbTouchEnd: ((Int) -> Void)?
|
open var onThumbTouchEnd: ((Int) -> Void)? {
|
||||||
|
get { nil }
|
||||||
|
set {
|
||||||
|
onThumbTouchEndCancellable?.cancel()
|
||||||
|
if let newValue {
|
||||||
|
onThumbTouchEndCancellable = onThumbTouchEndPublisher
|
||||||
|
.sink { c in
|
||||||
|
newValue(c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A publisher for when the thumb touch end. Passes parameters (position).
|
||||||
|
open var onThumbTouchEndPublisher = PassthroughSubject<Int, Never>()
|
||||||
|
private var onThumbTouchEndCancellable: AnyCancellable?
|
||||||
|
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - Private Properties
|
// MARK: - Private Properties
|
||||||
@ -250,13 +324,13 @@ open class CarouselScrollbar: View {
|
|||||||
func movePositionBackward() {
|
func movePositionBackward() {
|
||||||
position = position - 1
|
position = position - 1
|
||||||
scrollThumbToPosition(position)
|
scrollThumbToPosition(position)
|
||||||
self.onMoveBackward?(position)
|
onMoveBackwardPublisher.send(position)
|
||||||
}
|
}
|
||||||
|
|
||||||
func movePositionForward() {
|
func movePositionForward() {
|
||||||
position = position + 1
|
position = position + 1
|
||||||
scrollThumbToPosition(position)
|
scrollThumbToPosition(position)
|
||||||
self.onMoveForward?(position)
|
onMoveForwardPublisher.send(position)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compute track width and should maintain minimum thumb width if needed
|
// Compute track width and should maintain minimum thumb width if needed
|
||||||
@ -298,12 +372,13 @@ open class CarouselScrollbar: View {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private func scrollThumbToPosition(_ position: Int) {
|
private func scrollThumbToPosition(_ position: Int) {
|
||||||
self.setThumb(at: position)
|
setThumb(at: position)
|
||||||
self.onScrubberDrag?(position)
|
onScrubberDragPublisher.send(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: { [weak self] in
|
||||||
|
guard let self else { return }
|
||||||
self.thumbView.frame.origin.x = CGFloat(Float((position) - 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()
|
||||||
})
|
})
|
||||||
@ -318,7 +393,7 @@ open class CarouselScrollbar: View {
|
|||||||
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)
|
onThumbTouchStartPublisher.send(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)
|
||||||
@ -327,7 +402,7 @@ open class CarouselScrollbar: View {
|
|||||||
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 {
|
if sender.state == UIGestureRecognizer.State.ended {
|
||||||
self.onThumbTouchEnd?(position)
|
onThumbTouchEndPublisher.send(position)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user