【iOS】计算器实现

仿写iOS的计算器,使用了MVC模式和masonry库。MainView.hMainView.m添加事件MVC模式要求我们在MainViewController里实现并添加事件。我们在MainViewController例调用MainView的属性,为AC,等于和小数点这种特殊按钮单独添加事件。遍历MainView中储存普通按钮的数组,为它们添加一个总的事件。在这个总事件里,使用按钮的tag值进行分类,调用不同的事件。实现这些事件。限制输入在一定程度上限制用户的输入,
摘要由CSDN通过智能技术生成

仿写iOS的计算器,使用了MVC模式和masonry库。

绘制界面

  1. 循环建立button,统一设置属性,添加约束。
  2. 将括号按钮的tag设成100+,运算符按钮的tag设成200+,数字按钮的tag设成300+,用于之后分类。
  3. 设置不同按钮的颜色。
  4. 将AC,等于和小数点这种特殊按钮赋给mainView的属性。
  5. MainView声明一个数组属性,将除上面三个特殊按钮之外的按钮全部添加到数组里。
  6. 在按钮上方添加一个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 = 
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值