Skip to content

Commit

Permalink
Re add ios tap handler (#201)
Browse files Browse the repository at this point in the history
* Revert "Fix touch to seek flicker on iOS (#136)"

This reverts commit 71315cb.

* fix(ios): tap to slide scales values correctly

Also updates the example to listen to onSlidingComplete so the
tap events are handled well
  • Loading branch information
mikehardy committed Jul 11, 2020
1 parent 603ce23 commit 01342b8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions example/SliderExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class SliderExample extends React.Component<$FlowFixMeProps, $FlowFixMeState> {
<Slider
{...this.props}
onValueChange={value => this.setState({value: value})}
onSlidingComplete={value => this.setState({value: value})}
/>
</View>
);
Expand Down
16 changes: 15 additions & 1 deletion src/ios/RNCSlider.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,25 @@
@implementation RNCSlider
{
float _unclippedValue;
UITapGestureRecognizer * tapGesturer;
}

- (instancetype)initWithFrame:(CGRect)frame
{
return [super initWithFrame:frame];
self = [super initWithFrame:frame];
if (self) {
tapGesturer = [[UITapGestureRecognizer alloc] initWithTarget: self action:@selector(tapHandler:)];
[tapGesturer setNumberOfTapsRequired: 1];
[self addGestureRecognizer:tapGesturer];
}
return self;
}

- (void)tapHandler:(UITapGestureRecognizer *)gesture {
CGPoint touchPoint = [gesture locationInView:self];
float rangeWidth = self.maximumValue - self.minimumValue;
float sliderPercent = touchPoint.x / self.bounds.size.width;
[self setValue:self.minimumValue + (rangeWidth * sliderPercent) animated: YES];
}

- (void)setValue:(float)value
Expand Down

0 comments on commit 01342b8

Please sign in to comment.