所以我为此考虑了一个子类。事实证明它并没有那么糟糕(包装的值除外)。
使用子类
- (IBAction)stepperOneChanged:(UIStepper*)stepperOne
{
if (stepperOne.plusMinusState == JLTStepperPlus) {
// Plus button pressed
}
else if (stepperOne.plusMinusState == JLTStepperMinus) {
// Minus button pressed
} else {
// Shouldn't happen unless value is set programmatically.
}
}
JLTStepper.h
#import <UIKit/UIKit.h>
typedef enum JLTStepperPlusMinusState_ {
JLTStepperMinus = -1,
JLTStepperPlus = 1,
JLTStepperUnset = 0
} JLTStepperPlusMinusState;
@interface JLTStepper : UIStepper
@property (nonatomic) JLTStepperPlusMinusState plusMinusState;
@end
JLTStepper.m
#import "JLTStepper.h"
@implementation JLTStepper
- (void)setValue:(double)value
{
BOOL isPlus = self.value < value;
BOOL isMinus = self.value > value;
if (self.wraps) { // Handing wrapped values is tricky
if (self.value > self.maximumValue - self.stepValue) {
isPlus = value < self.minimumValue + self.stepValue;
isMinus = isMinus && !isPlus;
} else if (self.value < self.minimumValue + self.stepValue) {
isMinus = value > self.maximumValue - self.stepValue;
isPlus = isPlus && !isMinus;
}
}
if (isPlus)
self.plusMinusState = JLTStepperPlus;
else if (isMinus)
self.plusMinusState = JLTStepperMinus;
[super setValue:value];
}
@end