仿写iOS的计算器,使用了MVC模式和masonry库。
绘制界面
- 循环建立button,统一设置属性,添加约束。
- 将括号按钮的tag设成100+,运算符按钮的tag设成200+,数字按钮的tag设成300+,用于之后分类。
- 设置不同按钮的颜色。
- 将AC,等于和小数点这种特殊按钮赋给mainView的属性。
- MainView声明一个数组属性,将除上面三个特殊按钮之外的按钮全部添加到数组里。
- 在按钮上方添加一个UITextField,关掉它的用户交互。
MainView.h
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface MainView : UIView
@property (nonatomic, strong) NSArray* operArray;
@property (nonatomic, strong) NSMutableArray* buttonArray;
@property (nonatomic, strong) UITextField* calculation;
@property (nonatomic, strong) UIButton* ACButton;
@property (nonatomic, strong) UIButton* equalButton;
@property (nonatomic, strong) UIButton* pointButton;
- (void) initView;
@end
NS_ASSUME_NONNULL_END
MainView.m
#import "MainView.h"
#import "Masonry.h"
#define SIZE 85
#define CalculationWidth 375
@implementation MainView
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
- (void) initView {
self.backgroundColor = [UIColor blackColor];
[self creatOperArray];
self.buttonArray = [[NSMutableArray alloc] init];
self.calculation = [[UITextField alloc] init];
self.calculation.backgroundColor = [UIColor blackColor];
self.calculation.textColor = [UIColor whiteColor];
self.calculation.textAlignment = NSTextAlignmentRight;
self.calculation.font = [UIFont systemFontOfSize:60];
self.calculation.userInteractionEnabled = NO;
[self addSubview:self.calculation];
[self.calculation mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(self).offset(-600);
make.width.equalTo(@CalculationWidth);
}];
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.titleLabel.font = [UIFont systemFontOfSize:42];
button.layer.cornerRadius = SIZE / 2;
[self addSubview:button];
[button mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(self).offset(-(70 + (SIZE + 15) * (i + 1)));
make.left.equalTo(self).offset(3 + (SIZE + 15) * j);
make.size.equalTo(@SIZE);
}];
if (i == 3 && j < 3) {
button.backgroundColor = [UIColor grayColor];
[button setTitle:self.operArray[i + j + 1] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
button.tag = 100 + j;
if (j == 0) {
_ACButton = button;
} else {
[self.buttonArray addObject:button];
}
} else if (j == 3) {
button.backgroundColor = [UIColor colorWithRed:251.0f / 255.0f green:151.0f / 255.0f blue:15.0f / 255.0f alpha:1];
[button setTitle:self.operArray[i] forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
button.tag = 200 + i;
[self.buttonArray addObject:button];
} else {
button.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1];
NSString* title = [NSString stringWithFormat:@"%d", j + 3 * i + 1];
[button setTitle:title forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
button.tag = 300 + i * 3 + j;
[self.buttonArray addObject:button];
}
}
}
for (int i = 0; i < 3; i++) {
UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.titleLabel.font = [UIFont systemFontOfSize:42];
button.layer.cornerRadius = SIZE / 2;
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self addSubview:button];
if (i == 0) {
[button mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(self).offset(-70);
make.left.equalTo(self).offset(0);
make.width.equalTo(@(SIZE * 2 + 15));
make.height.equalTo(@SIZE);
}];
[button setTitle:@"0 " forState:UIControlStateNormal];
button.backgroundColor = [UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1];
button.tag = 310;
[self.buttonArray addObject:button];
} else {
[button mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(self).offset(-70);
make.left.equalTo(self).offset(200 + (SIZE + 15) * (i - 1));
make.size.equalTo(@SIZE);
}];
if (i == 1) {
[button setTitle:@"." forState:UIControlStateNormal];
button.backgroundColor =