这就是我目前正在做的事情
- (IBAction)sliderDistanceChanged {
CGFloat sliderPosition = self.sliderDistance.value;
NSUInteger sliderIndex = [BGDistanceFilterStop indexForFilterEffect:sliderPosition];
self.distanceFilterForCatalog = @([BGDistanceFilterStop numberValueOfDistance:sliderIndex]);
//self.distanceFilterForCatalog= @(self.sliderDistance.value).roundWithForDistanceFilter;
//[self.distanceFilter setTitle:self.distanceFilterForCatalog.distanceForm forState:UIControlStateNormal];
[self.distanceFilter setTitle:[BGDistanceFilterStop stringValueOfDistance:sliderIndex] forState:UIControlStateNormal];
}
- (IBAction)SliderNoLongerTouched:(id)sender {
CGFloat sliderIndex = [BGDistanceFilterStop sliderSnappedPosition:self.sliderDistance.value];
self.sliderDistance.value=sliderIndex;
}
基本上它会检测self.sliderDistance.value,然后根据该值设置舍入后的报告值。
一旦用户不再触摸,我就改变了self.sliderDistance.value的位置
没关系。
但是,我想在用户修改之前更改self.sliderDistance.value。但是,它与iOS用于跟踪手的位置和滑块值的变量相同。
那么如何安排按钮对齐而不更改self.sliderDistance.value?
答案 0 :(得分:2)
我假设这些操作中的每一个都与相关的UISlider相关联。第一个由valueChanged
触发,第二个由touchUpInside
触发。
在这种情况下,首先将您的签名更改为
- (IBAction)sliderDistanceChanged:(UISlider*)sender;
- (IBAction)sliderNoLongerTouched:(UISlider*)sender;
这样您就可以使用sender
代替self.sliderDistance
来引用滑块(更简单,更自包含)。
修改sliderDistanceChanged
- (IBAction)sliderDistanceChanged:(UISlider*)sender;
//allocate the raw slider value to a variable
CGFloat rawValue = sender.value;
//calculate the snapped value
CGFloat snappedValue = [BGDistanceFilterStop sliderSnappedPosition:sender.value];
//snap the slider in position
sender.value = snappedValue;
//now do the rest of your processing
//using rawValue if you want the raw value,
//snappedValue if you want the snapped value
}
简化sliderNoLongerTouched
- (IBAction)sliderNoLongerTouched:(id)sender {
sender.value = [BGDistanceFilterStop sliderSnappedPosition:sender.value];
}
(你现在实际上并不需要这个,因为价值在sliderDistanceChanged
中占据了一席之地)
答案 1 :(得分:0)
(IBAction)sliderDistanceChanged {
CGFloat sliderPosition = self.sliderDistance.value;
NSUInteger sliderIndex = [BGDistanceFilterStop indexForFilterEffect:sliderPosition];
self.distanceFilterForCatalog = @([BGDistanceFilterStop numberValueOfDistance:sliderIndex]);
//self.distanceFilterForCatalog= @(self.sliderDistance.value).roundWithForDistanceFilter;
//[self.distanceFilter setTitle:self.distanceFilterForCatalog.distanceForm forState:UIControlStateNormal];
[self.distanceFilter setTitle:[BGDistanceFilterStop stringValueOfDistance:sliderIndex] forState:UIControlStateNormal];
}
- (IBAction)SliderNoLongerTouched:(id)sender {
CGFloat sliderIndex = [BGDistanceFilterStop sliderSnappedPosition:self.sliderDistance.value];
self.sliderDistance.value=sliderIndex;
}
基本上它会检测self.sliderDistance.value
然后根据该设置在舍入后报告的值。
用户不再触摸后,我会更改self.sliderDistance.value
没关系。
何