• 【iOS】计算器实现


    仿写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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    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 = 
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
  • 相关阅读:
    Day08--初步创建并使用自定义组件
    Go kafka
    LVS集群-DR模式【部署高可用LVS-DR集群】
    C#扩展——自定义VS公/私有方法代码片段
    动态规划无效总结--回文子串,最大子数和
    【网络安全】网络安全基础精讲 - 网络安全入门第一篇
    自举电容充电回路分析
    中国顶级CTF竞赛网络安全大赛--2022网鼎杯re2解题思路来了,快来围观!
    nginx——复习
    Cmd命令获取结果
  • 原文地址:https://blog.csdn.net/m0_63852285/article/details/127129013