gpt4 book ai didi

iphone - 实现步骤/捕捉 UISlider

转载 作者:行者123 更新时间:2023-12-03 18:11:20 27 4
gpt4 key购买 nike

我正在尝试使用 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com