仿淘宝购物车计数器
我使用的Masonary布局,當然這里可以改成frame,很簡單,新建CountView繼承UIView,CreateUI這個類是我封裝創建基礎的UILabel,UIButton的,很簡單的代碼
/*項目新增數量計數器*/ #import <UIKit/UIKit.h> @interface CountView : UIView//如果點擊+或者-,把當前的計數傳出去 @property(nonatomic,copy)void(^getNumber)(int number); @end
?#import "CountView.h"#import "CreateUI.h"#import "Masonry.h"
?
#import "CountView.h" #import "CreateUI.h" //#import "DefineStr.h" #import "Masonry.h"#define BackColor @"#d6d6d6" @interface CountView () @property(nonatomic,strong)UIButton *subBtn; @property(nonatomic,strong)UILabel *numberLab; @property(nonatomic,strong)UIButton *addBtn; @end@implementation CountView-(instancetype)initWithFrame:(CGRect)frame{self = [super initWithFrame:frame];if(self){//給當前View加圓角[CreateUI radiuCornerWithColor:self radius:3.0 colorStr:BackColor];//創建UIButtonself.subBtn = [CreateUI createBtnWithTile:@"-" withTitleColorStr:@"#2a2a2a" withFontSize:12];[self addSubview:self.subBtn];//創建UILabelself.numberLab = [CreateUI createLabelWithTextColorStr:@"#2a2a2a" withFontSize:12 withText:@"0"];[self addSubview:self.numberLab];self.addBtn = [CreateUI createBtnWithTile:@"+" withTitleColorStr:@"#2a2a2a" withFontSize:12];[self addSubview:self.addBtn];[self.subBtn setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];[CreateUI addFrameWithRightView:self.subBtn borderWidth:1.0f withBorderColor:BackColor];[CreateUI addFrameWithRightView:self.numberLab borderWidth:1.0f withBorderColor:BackColor];[self.addBtn setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];[self.subBtn setEnabled:NO];[self.subBtn mas_makeConstraints:^(MASConstraintMaker *make) {make.left.top.bottom.offset(0);make.width.equalTo(self.mas_width).multipliedBy(2/7.0);}];self.numberLab.textAlignment = NSTextAlignmentCenter;[self.numberLab mas_makeConstraints:^(MASConstraintMaker *make) {make.left.equalTo(self.subBtn.mas_right);make.top.bottom.offset(0);make.right.equalTo(self.addBtn.mas_left);}];[self.addBtn mas_makeConstraints:^(MASConstraintMaker *make) {make.left.equalTo(self.subBtn.mas_right);make.top.bottom.right.offset(0);make.width.equalTo(self.subBtn.mas_width);}];[self.subBtn addTarget:self action:@selector(subBtnClick) forControlEvents:UIControlEventTouchUpInside];[self.addBtn addTarget:self action:@selector(addBtnClick) forControlEvents:UIControlEventTouchUpInside];}return self; } //點擊-號事件 -(void)subBtnClick{int number = [self.numberLab.text intValue]-1;self.numberLab.text = [NSString stringWithFormat:@"%d",number];//如果已經是0了,則-號不能點擊if([self.numberLab.text intValue] == 0){[self.subBtn setEnabled:NO];}if(self.getNumber){self.getNumber(number);} } //點擊+號事件 -(void)addBtnClick{int number = [self.numberLab.text intValue]+1;self.numberLab.text = [NSString stringWithFormat:@"%d",number];//如果-號不能點擊,當前計數大于0,則-號UIButton可點擊if(![self.subBtn isEnabled]){[self.subBtn setEnabled:YES];}if(self.getNumber){self.getNumber(number);} }@end?
?
?
?
使用:
CountView *view = [CountView new];[self.view addSubview:view];[view mas_makeConstraints:^(MASConstraintMaker *make) {make.top.offset(500);make.left.offset(100);make.width.offset(100);make.height.offset(50);}];view.getNumber = ^(int number) {NSLog(@"----:%d",number);};?
轉載于:https://www.cnblogs.com/hualuoshuijia/p/9967757.html
總結
- 上一篇: 【题解】CF#611 H-New Yea
- 下一篇: 142-练习8和9 for循环的嵌套调用