-
Notifications
You must be signed in to change notification settings - Fork 296
feat(ios): add swipeToSeek #750
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -24,6 +24,7 @@ @implementation RNCSliderComponentView | |||
| RNCSlider *slider; | ||||
| UIImage *_image; | ||||
| BOOL _isSliding; | ||||
| BOOL _swipeGestureEnabled; | ||||
| } | ||||
|
|
||||
| + (ComponentDescriptorProvider)componentDescriptorProvider | ||||
|
|
@@ -202,6 +203,9 @@ - (void)updateProps:(const Props::Shared &)props oldProps:(const Props::Shared & | |||
| if (oldScreenProps.tapToSeek != newScreenProps.tapToSeek) { | ||||
| slider.tapToSeek = newScreenProps.tapToSeek; | ||||
| } | ||||
| if (oldScreenProps.swipeToSeek != newScreenProps.swipeToSeek) { | ||||
| [self setSwipeToSeek:newScreenProps.swipeToSeek]; | ||||
| } | ||||
| if (oldScreenProps.minimumValue != newScreenProps.minimumValue) { | ||||
| [slider setMinimumValue:newScreenProps.minimumValue]; | ||||
| } | ||||
|
|
@@ -298,6 +302,78 @@ - (void)setInverted:(BOOL)inverted | |||
| } | ||||
| } | ||||
|
|
||||
| #pragma mark - Swipe to seek | ||||
|
|
||||
| - (void)setSwipeToSeek:(BOOL)swipeToSeek | ||||
| { | ||||
| if (swipeToSeek && !_swipeGestureEnabled) { | ||||
| UIPanGestureRecognizer *panGesturer; | ||||
| panGesturer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panHandler:)]; | ||||
| [slider addGestureRecognizer:panGesturer]; | ||||
| _swipeGestureEnabled = YES; | ||||
| } | ||||
| } | ||||
|
|
||||
| - (void)panHandler:(UIPanGestureRecognizer *)gesture { | ||||
| CGPoint location = [gesture locationInView:slider]; | ||||
|
|
||||
| switch (gesture.state) { | ||||
|
|
||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
| case UIGestureRecognizerStateBegan: { | ||||
| [self updateSliderToLocation:location]; | ||||
| std::dynamic_pointer_cast<const RNCSliderEventEmitter>(_eventEmitter) | ||||
| ->onRNCSliderSlidingStart(RNCSliderEventEmitter::OnRNCSliderSlidingStart{.value = static_cast<Float>(slider.lastValue)}); | ||||
| break; | ||||
| } | ||||
|
|
||||
| case UIGestureRecognizerStateChanged: { | ||||
| [self updateSliderToLocation:location]; | ||||
| break; | ||||
| } | ||||
|
|
||||
| case UIGestureRecognizerStateEnded: | ||||
| case UIGestureRecognizerStateCancelled: | ||||
| case UIGestureRecognizerStateFailed: { | ||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't we want to avoid sending an event if the state is failed?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i interpreted |
||||
| std::dynamic_pointer_cast<const RNCSliderEventEmitter>(_eventEmitter) | ||||
| ->onRNCSliderSlidingComplete(RNCSliderEventEmitter::OnRNCSliderSlidingComplete{.value = static_cast<Float>(slider.value)}); | ||||
| break; | ||||
| } | ||||
|
|
||||
| default: | ||||
| break; | ||||
| } | ||||
| } | ||||
|
|
||||
| - (void)updateSliderToLocation:(CGPoint)location { | ||||
| float newValue = [self calculateSliderValueFromLocation:location]; | ||||
| float discreteValue = [slider discreteValue:newValue]; | ||||
|
|
||||
| [slider setValue:newValue animated:NO]; | ||||
|
|
||||
| if (discreteValue != slider.lastValue) { | ||||
| std::dynamic_pointer_cast<const RNCSliderEventEmitter>(_eventEmitter) | ||||
| ->onRNCSliderValueChange(RNCSliderEventEmitter::OnRNCSliderValueChange{.value = static_cast<Float>(slider.value)}); | ||||
| } | ||||
|
|
||||
| slider.lastValue = discreteValue; | ||||
| } | ||||
|
|
||||
| - (float)calculateSliderValueFromLocation:(CGPoint)point { | ||||
| CGFloat sliderWidth = slider.bounds.size.width; | ||||
|
|
||||
| if (sliderWidth <= 0) { | ||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When such case is possible?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good catch! seems like an overkill guard given this path only gets invoked on slider changes |
||||
| return slider.value; | ||||
| } | ||||
|
|
||||
| CGFloat percentage = point.x / sliderWidth; | ||||
| percentage = MIN(1.0, MAX(0.0, percentage)); | ||||
|
|
||||
| CGFloat range = slider.maximumValue - slider.minimumValue; | ||||
| float newValue = slider.minimumValue + (percentage * range); | ||||
|
|
||||
| return newValue; | ||||
| } | ||||
|
|
||||
| @end | ||||
|
|
||||
| Class<RCTComponentViewProtocol> RNCSliderCls(void) | ||||
|
|
||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sterlingwes Can you elaborate on why we need this private variable?
From what I see it's only used once in the whole runtime to set the gesture recognizer. But is this required?
If we could make the
swipeToSeekbeingtrueby default, we wouldn't need this additional var check.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
totally open to making this the default behaviour and not needing the tracking bool 👍🏻
the opt-in prop was intended to allow this as a minor release but if you're planning on a major and open to the behavioural change, makes sense to me to simplify for sure
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hold on, a major? I will need to double check if this behavior was there in the old Sliders, or if my memory if faulty here 🤔
Thanks for the comment!