我有一个客户,希望我修改UIStepper的自动重复行为。

如果用户选择触摸并按住步进器的+按钮,则步进器的值开始以每半秒左右的1的速率增加,然后在大约3秒钟后,值开始更加迅速地变化。

无论如何,是否需要修改它的工作方式,例如,如果用户点击并按住,这些值将立即以更快的速度增加?

我看过UIStepped文档,但没有看到任何相关信息,但是我想知道是否有一种方法可以通过IBAction或其他方式实现。

最佳答案

首先,为步进器添加两个操作:

[theStepper addTarget:self action:@selector(stepperTapped:) forControlEvents:UIControlEventTouchDown];
[theStepper addTarget:self action:@selector(stepperValueChanged:) forControlEvents:UIControlEventValueChanged];

这些动作如下所示:
- (IBAction)stepperTapped:(id)sender {
self.myStepper.stepValue = 1;
self.myStartTime = CFAbsoluteTimeGetCurrent();

}
- (IBAction)stepperValueChanged:(id)sender {
self.myStepper.stepValue = [self stepValueForTimeSince:self.myStepperStartTime];
// handle the value change here

}

这是魔术代码:
- (double)stepValueForTimeSince:(CFAbsoluteTime)aStartTime {
double theStepValue = 1;
CFAbsoluteTime  theElapsedTime  = CFAbsoluteTimeGetCurrent() - aStartTime;

if (theElapsedTime > 6.0) {
    theStepValue = 1000;
} else if (theElapsedTime > 4.0) {
    theStepValue = 100;
} else if (theElapsedTime > 2.0) {
    theStepValue = 10;
}

return theStepValue;

}

10-07 19:14
查看更多