iOS可復用控制項之調節控制項

2021-08-09 07:30:10 字數 2994 閱讀 3438

效果:

實現:

//  htadjustview.h

#import @inte***ce htadjustview : uiview

// 設定名稱、當前值、單位、上限值、下限值

- (void)setname:(nsstring *)name

value:(cgfloat)value

unit:(nsstring *)unit

maxvalue:(cgfloat)maxvalue

minvalue:(cgfloat)minvalue;

@end

//  htadjustview.m

#import "htadjustview.h"

@inte***ce htadjustview ()

@property (nonatomic, strong) uiimageview *adjustview; // 調節按鈕

@property (nonatomic, strong) uiimageview *adjustbackview; // 背景檢視

@property (nonatomic, assign) bool preparetomove; // 調節按鈕是否可以移動

@property (nonatomic, strong) uibutton *valuebtn; // 當前值

@property (nonatomic, strong) uibutton *namebtn; // 名稱

@property (nonatomic, copy) nsstring *unit; // 單位

@property (nonatomic, assign) cgfloat maxvalue; // 上限值

@property (nonatomic, assign) cgfloat minvalue; // 下限值

@property (nonatomic, assign) cgfloat maxvaluey; // 最大值位置的y座標

@property (nonatomic, assign) cgfloat minvaluey; // 最小值位置的y座標

@property (nonatomic, assign) cgfloat displayvalue; // 當前用於展示的值

@property (nonatomic, strong) uiimageview *adjustprogressview; // 進度條

@property (nonatomic, strong) uiimageview *scaleview; // 刻度檢視

@end

@implementation htadjustview

- (instancetype)initwithframe:(cgrect)frame

return self;

}// 初始化檢視

- (void)loadviews

- (void)touchesbegan:(nsset*)touches withevent:(uievent *)event

}- (void)touchesmoved:(nsset*)touches withevent:(uievent *)event else if (currentcentery > self.minvaluey)

[self.adjustview setcenter:cgpointmake(self.adjustview.center.x, currentcentery)];

// 進度條跟隨調節按鈕變化

self.adjustprogressview.frame = cgrectmake(0, currentcentery - self.adjustbackview.frame.origin.y, self.adjustprogressview.frame.size.width, self.adjustprogressview.frame.size.height);

// 根據上下限計算展示值

cgfloat ratio = (self.minvaluey - currentcentery)/totalvalueheight;

self.displayvalue = ratio * (self.maxvalue - self.minvalue) + self.minvalue;

nsstring *valuestr = [nsstring stringwithformat:@"%.1f%@", self.displayvalue, self.unit];

[self.valuebtn settitle:valuestr forstate:uicontrolstatenormal];

}}- (void)touchesended:(nsset*)touches withevent:(uievent *)event

// 設定名稱、展示值、單位、上限值、下限值

- (void)setname:(nsstring *)name

value:(cgfloat)value

unit:(nsstring *)unit

maxvalue:(cgfloat)maxvalue

minvalue:(cgfloat)minvalue

@end

使用:

//  viewcontroller.m

#import "viewcontroller.h"

#import "htadjustview.h"

@inte***ce viewcontroller ()

@end

@implementation viewcontroller

- (void)viewdidload

- (void)didreceivememorywarning

@end

iOS可復用控制項之表盤

效果 實現 實際應用中,整個區域經常需要分為偏低區域 適中區域 偏高區域等不同的劃分區域,要用不同的顏色標識,這裡我們預設劃分低 中 高三個區域,所以需要兩個區域的間隔值 htdialview.h import inte ce htdialview uiview property nonatomic...

iOS學習之UIPickerView控制項的簡單使用

uipickerview控制項在給使用者選擇某些特定的資料時經常使用到,這裡演示乙個簡單的選擇資料,顯示在uitextfield輸入框裡,把uipickerview作為輸入view,用 作為選定資料的按鈕。和其他uitableview控制項相似,uipickerview也需要資料來源。我們要實現的效...

IOS控制項學習之UILabel

ios的uilabel我個人覺得很弱智,居然只有橫向設定而沒有豎向設定文字位置,這讓我很糾結。先看效果 1.新建專案 2.編輯 viewcontroller.m檔案 void viewdidload 3.自定義uilabelex 控制項 1 uilabelex.h import 定義乙個列舉型別,頂...