-
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? |
||||
| 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? |
||||
| 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.