1. 程式人生 > 其它 >OC Control PPNumberButton

OC Control PPNumberButton

技術標籤:UI的封裝iosobjective-cswiftxcode

一直覺得自己寫的不是技術,而是情懷,一個個的教程是自己這一路走來的痕跡。靠專業技能的成功是最具可複製性的,希望我的這條路能讓你們少走彎路,希望我能幫你們抹去知識的蒙塵,希望我能幫你們理清知識的脈絡,希望未來技術之巔上有你們也有我。

使用場景:電商專案加減商品進購物車
在這裡插入圖片描述

控制元件所提供的方法

- (instancetype)initWithFrame:(CGRect)frame;
+ (instancetype)numberButtonWithFrame:(CGRect)frame;

/** 加減按鈕的Block回撥*/
@property (nonatomic, copy) void(^resultBlock)(PPNumberButton *ppBtn,CGFloat number, BOOL increaseStatus/* 是否為加狀態*/); /** 代理*/ @property (nonatomic, weak) id<PPNumberButtonDelegate> delegate; #pragma mark - 自定義樣式屬性設定 /** 是否開啟抖動動畫, default is NO*/ @property (nonatomic, assign ) IBInspectable BOOL shakeAnimation;
/** 為YES時,初始化時減號按鈕隱藏(餓了麼/百度外賣/美團外賣按鈕模式),default is NO*/ @property (nonatomic, assign ) IBInspectable BOOL decreaseHide; /** 是否可以使用鍵盤輸入,default is YES*/ @property (nonatomic, assign, getter=isEditing) IBInspectable BOOL editing; /** 設定邊框的顏色,如果沒有設定顏色,就沒有邊框 */ @property (nonatomic, strong ) IBInspectable UIColor *
borderColor; /** 輸入框中的內容 */ @property (nonatomic, assign ) CGFloat currentNumber; /** 遞增步長,預設步長為1 */ @property (nonatomic, assign ) CGFloat stepValue; /** 輸入框中的字型大小 */ @property (nonatomic, assign ) IBInspectable CGFloat inputFieldFont; /** 長按加減的時間間隔,預設0.1s,設定為 CGFLOAT_MAX 則關閉長按加減功能*/ @property (nonatomic, assign ) IBInspectable CGFloat longPressSpaceTime; /** 加減按鈕的字型大小 */ @property (nonatomic, assign ) IBInspectable CGFloat buttonTitleFont; /** 加按鈕背景圖片 */ @property (nonatomic, strong ) IBInspectable UIImage *increaseImage; /** 減按鈕背景圖片 */ @property (nonatomic, strong ) IBInspectable UIImage *decreaseImage; /** 加按鈕標題 */ @property (nonatomic, copy ) IBInspectable NSString *increaseTitle; /** 減按鈕標題 */ @property (nonatomic, copy ) IBInspectable NSString *decreaseTitle; /** 最小值, default is 0 */ @property (nonatomic, assign ) IBInspectable CGFloat minValue; /** 最大值 */ @property (nonatomic, assign ) CGFloat maxValue; /** 目前支援一位小數的遞增 */ @property (nonatomic, assign ) BOOL decimalNum; @end #pragma mark - NSString分類 @interface NSString (PPNumberButton) /** 字串 nil, @"", @" ", @"\n" Returns NO; 其他 Returns YES. */ - (BOOL)pp_isNotBlank;

使用

PPNumberButtonDelegate


@property (nonatomic,strong) PPNumberButton *numberBtn;

  self.numberBtn = [PPNumberButton new];
  self.numberBtn.shakeAnimation = YES;
  self.numberBtn.editing = YES;
  self.numberBtn.borderColor = [Color line];
  self.numberBtn.currentNumber = 1;
  self.numberBtn.inputFieldFont = 16;
  self.numberBtn.longPressSpaceTime = 0.5;
  self.numberBtn.buttonTitleFont = 16;
  self.numberBtn.increaseTitle = @"+";
  self.numberBtn.decreaseTitle = @"-";
  self.numberBtn.minValue = 1;
  self.numberBtn.stepValue = 1;
  self.numberBtn.delegate = self;
  self.numberBtn.addTo(self.bgView).makeCons(^{
    make.right.equal.view(self.bgView).constants(-10);
    make.top.equal.view(self.labelView).bottom.constants(5);
    make.height.equal.constants(30);
    make.width.equal.constants(90);
  });
  
#pragma  mark LabelView  用於返回選中數量
- (void)pp_numberButton:(PPNumberButton *)numberButton number:(NSInteger)number increaseStatus:(BOOL)increaseStatus{

}

效果
在這裡插入圖片描述