mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-29 04:35:36 +08:00
Improved SliderIOS
Summary: public * No longer sends events when not observing valueChanged. * Snaps to step value while dragging. * Added additional example to UIExplorer. Reviewed By: javache Differential Revision: D2595594 fb-gh-sync-id: 1e92427d2ab2e71e4eb4a9a7a75cd0f5f4a3a529
This commit is contained in:
committed by
facebook-github-bot-3
parent
2fa58b58f9
commit
0d14d0f1ce
@@ -13,7 +13,9 @@
|
||||
|
||||
@interface RCTSlider : UISlider
|
||||
|
||||
@property (nonatomic, copy) RCTBubblingEventBlock onChange;
|
||||
@property (nonatomic, copy) RCTBubblingEventBlock onValueChange;
|
||||
@property (nonatomic, copy) RCTBubblingEventBlock onSlidingComplete;
|
||||
|
||||
@property (nonatomic, assign) float step;
|
||||
@property (nonatomic, assign) float lastValue;
|
||||
|
||||
|
||||
@@ -21,10 +21,12 @@ RCT_EXPORT_MODULE()
|
||||
- (UIView *)view
|
||||
{
|
||||
RCTSlider *slider = [RCTSlider new];
|
||||
[slider addTarget:self action:@selector(sliderValueChanged:) forControlEvents:UIControlEventValueChanged];
|
||||
[slider addTarget:self action:@selector(sliderTouchEnd:) forControlEvents:(UIControlEventTouchUpInside |
|
||||
UIControlEventTouchUpOutside |
|
||||
UIControlEventTouchCancel)];
|
||||
[slider addTarget:self action:@selector(sliderValueChanged:)
|
||||
forControlEvents:UIControlEventValueChanged];
|
||||
[slider addTarget:self action:@selector(sliderTouchEnd:)
|
||||
forControlEvents:(UIControlEventTouchUpInside |
|
||||
UIControlEventTouchUpOutside |
|
||||
UIControlEventTouchCancel)];
|
||||
return slider;
|
||||
}
|
||||
|
||||
@@ -34,23 +36,29 @@ static void RCTSendSliderEvent(RCTSlider *sender, BOOL continuous)
|
||||
|
||||
if (sender.step > 0 &&
|
||||
sender.step <= (sender.maximumValue - sender.minimumValue)) {
|
||||
|
||||
value =
|
||||
MAX(sender.minimumValue,
|
||||
MIN(sender.maximumValue,
|
||||
sender.minimumValue + round((sender.value - sender.minimumValue) / sender.step) * sender.step
|
||||
)
|
||||
);
|
||||
|
||||
[sender setValue:value animated:YES];
|
||||
}
|
||||
|
||||
if (!continuous) {
|
||||
sender.value = value;
|
||||
}
|
||||
|
||||
if (sender.onChange && (sender.lastValue != value || !continuous)) {
|
||||
sender.onChange(@{
|
||||
@"value": @(value),
|
||||
@"continuous": @(continuous),
|
||||
});
|
||||
if (continuous) {
|
||||
if (sender.onValueChange && sender.lastValue != value) {
|
||||
sender.onValueChange(@{
|
||||
@"value": @(value),
|
||||
});
|
||||
}
|
||||
} else {
|
||||
if (sender.onSlidingComplete) {
|
||||
sender.onSlidingComplete(@{
|
||||
@"value": @(value),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
sender.lastValue = value;
|
||||
@@ -72,7 +80,8 @@ RCT_EXPORT_VIEW_PROPERTY(minimumValue, float);
|
||||
RCT_EXPORT_VIEW_PROPERTY(maximumValue, float);
|
||||
RCT_EXPORT_VIEW_PROPERTY(minimumTrackTintColor, UIColor);
|
||||
RCT_EXPORT_VIEW_PROPERTY(maximumTrackTintColor, UIColor);
|
||||
RCT_EXPORT_VIEW_PROPERTY(onChange, RCTBubblingEventBlock);
|
||||
RCT_EXPORT_VIEW_PROPERTY(onValueChange, RCTBubblingEventBlock);
|
||||
RCT_EXPORT_VIEW_PROPERTY(onSlidingComplete, RCTBubblingEventBlock);
|
||||
RCT_CUSTOM_VIEW_PROPERTY(disabled, BOOL, RCTSlider)
|
||||
{
|
||||
if (json) {
|
||||
|
||||
Reference in New Issue
Block a user