您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 中 UIStepper 數值加減器的屬性與用法指南

編輯:iOS 開發綜合

UIStepper 用來讓使用者連續遞增或遞減一個數值。外觀是兩個水平並排的按鈕,一個顯示「+」,一個顯示「-」。

它最有趣的地方在於長按會加速:按住「+」或「-」越久,數值變動得越快。這讓它特別適合調整「範圍較大、但需要粗略定位」的數值,例如音量、字級大小、重複次數。你可以為它設定一個範圍,例如 0 到 99。

一、核心屬性

· value:目前的值,預設 0.0
· minimumValue:可表示的最小值,預設 0.0
· maximumValue:可表示的最大值,預設 100.0
· stepValue:每次遞增/遞減的量,預設 1.0

二、如何判斷使用者是加還是減

UIStepper 本身不告訴你方向,只給你當前值。做法是自己記住上一次的值再比較:

double previousValue;   // 用來記錄 stepper.value 的上一次值

#pragma mark - 建立 UIStepper

- (void)createUIStepper {
    UIStepper *stepperButton = [[UIStepper alloc] initWithFrame:CGRectMake(225, 500, 30, 10)];
    [stepperButton addTarget:self action:@selector(controlStepperValue:)
            forControlEvents:UIControlEventValueChanged];

    stepperButton.maximumValue = 100.0;
    stepperButton.minimumValue = 0.0;
    stepperButton.value = INITUISTEPPERVALUE;
    stepperButton.stepValue = 1.0;
    stepperButton.continuous = YES;   // 長按連續觸發
    stepperButton.wraps = NO;         // 不循環
    stepperButton.autorepeat = YES;   // 長按持續變動
    stepperButton.tintColor = [UIColor blueColor];

    [self.view addSubview:stepperButton];
}

#pragma mark - 判斷方向

- (void)controlStepperValue:(UIStepper *)stepper {
    BOOL isIncrease = (stepper.value > previousValue);

    if (_segment.selectedSegmentIndex == 0) {
        CGRect redRect = _redView.frame;
        redRect.size.height += isIncrease ? 5 : -5;
        _redView.frame = redRect;
    } else {
        CGRect greenRect = _greenView.frame;
        greenRect.size.height += isIncrease ? 5 : -5;
        _greenView.frame = greenRect;
    }

    previousValue = stepper.value;   // 關鍵:更新記錄值
}

注意最後那一行——忘了更新 previousValue,方向判斷就會永遠指向同一邊。

三、屬性速查

continuous

@property(nonatomic, getter=isContinuous) BOOL continuous;

設為 YES 時長按會連續觸發;設為 NO 則只在手指放開後觸發一次。

autorepeat

@property(nonatomic) BOOL autorepeat;

設為 YES 時長按會持續變值;設為 NO 則一次點擊只變一次。

wraps

@property(nonatomic) BOOL wraps;

設為 YES 時,值到達上下界會循環回到另一端,預設 NO。

值與範圍

@property(nonatomic) double value;
@property(nonatomic) double minimumValue;   // 預設 0
@property(nonatomic) double maximumValue;   // 預設 100
@property(nonatomic) double stepValue;

外觀客製

@property(nonatomic, retain) UIColor *tintColor;

- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state;
- (UIImage *)backgroundImageForState:(UIControlState)state;

- (void)setDividerImage:(UIImage *)image forLeftSegmentState:(UIControlState)leftState
     rightSegmentState:(UIControlState)rightState;
- (UIImage *)dividerImageForLeftSegmentState:(UIControlState)state
                            rightSegmentState:(UIControlState)state;

- (void)setIncrementImage:(UIImage *)image forState:(UIControlState)state;
- (UIImage *)incrementImageForState:(UIControlState)state;

- (void)setDecrementImage:(UIImage *)image forState:(UIControlState)state;
- (UIImage *)decrementImageForState:(UIControlState)state;

這些 API 讓你能完全替換 UIStepper 的外觀——背景、左右分割線、加號圖、減號圖各自獨立設定,而且都支援 UIControlState,因此按下與放開可以有不同樣式。需要注意的是,一旦自訂了背景與按鈕圖,tintColor 就不再生效。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved