如何使应用程序UISlider快速?

时间:2013-02-18 07:57:55

标签: objective-c cocoa-touch

这就是我目前正在做的事情

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

2 个答案:

答案 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)

enter image description here

(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

的位置

没关系。