iphone - 实现步骤/捕捉 UISlider

标签 iphone uislider

我正在尝试使用 UISlider 实现某种形式的捕捉或步骤。我编写了以下代码,但它并没有像我希望的那样顺利。它可以工作,但是当我向上滑动它时,它会向右移动 5 个点,使手指不在“滑动圆”的中心

这是我的代码,其中 self.lastQuestionSliderValue 是我已将其设置为 slider 初始值的类的属性。

    if (self.questionSlider.value > self.lastQuestionSliderValue) {
        self.questionSlider.value += 5.0;
    } else {
        self.questionSlider.value -= 5.0;
    }

    self.lastQuestionSliderValue = (int)self.questionSlider.value;

最佳答案

这实际上比我最初想象的要容易得多。最初我试图获得拇指矩形属性并进行复杂的数学运算。这是我最终得到的结果:

h 文件:

@property (nonatomic, retain) IBOutlet UISlider* questionSlider;
@property (nonatomic) float lastQuestionStep;
@property (nonatomic) float stepValue;

m 文件:

- (void)viewDidLoad {
    [super viewDidLoad];

    // Set the step to whatever you want. Make sure the step value makes sense
    //   when compared to the min/max values for the slider. You could take this
    //   example a step further and instead use a variable for the number of
    //   steps you wanted.
    self.stepValue = 25.0f;

    // Set the initial value to prevent any weird inconsistencies.
    self.lastQuestionStep = (self.questionSlider.value) / self.stepValue;
}

// This is the "valueChanged" method for the UISlider. Hook this up in
//   Interface Builder.
-(IBAction)valueChanged:(id)sender {
    // This determines which "step" the slider should be on. Here we're taking 
    //   the current position of the slider and dividing by the `self.stepValue`
    //   to determine approximately which step we are on. Then we round to get to
    //   find which step we are closest to.
    float newStep = roundf((questionSlider.value) / self.stepValue);

    // Convert "steps" back to the context of the sliders values.
    self.questionSlider.value = newStep * self.stepValue;
}

确保您连接了 UISlider View 的方法和 socket ,然后就可以开始了。

关于iphone - 实现步骤/捕捉 UISlider,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6370342/

相关文章:

iphone - iPhone 上的开源用户跟踪

ios - iPhone - 调整图像和两个标签以适应屏幕

jquery - 通过使用带有 Fontawesome 的 jQuery UI slider 更改星级

swift - XCode 8 Beta 6 中引入的 UISlider 错误的解决方法?

ios - 应用程序启动后加载不同的 View Controller

iphone - XCode 4.2 和 SDK 4.3 有自定义按钮字体问题吗?

ios - UIWebView 从后台返回后在 iOS 7 上更改 contentSize

iphone - UISlider跟踪图像不拉伸(stretch)

ios - 查找时如何更改 UISlider 拇指的大小

javascript - 未捕获的类型错误 : Cannot read property 'addClass' of undefined in jquery slider