ios搭一个简易计算器(利用masonry布局)
最后更新于:2022-04-01 10:06:47
这几天学习masonry,搭了一个计算器的界面,的确很简单方便,本着新手的认学态度,索性把计算器功能做出来,没想到啊,新手就是新手,弄了三天才算有所小成。代码放出来给大家参考一下,有不足之处还请留言教我。废话不说,下面上代码
~~~
#import "MainViewController.h"
#import "Masonry.h"
@interfaceMainViewController ()
{
UITextField * utf;//输出数字的
UIButton * btn;//按钮部分,不包括数字0
UIButton * btn1;//按钮:数字0
UILabel * line;//按钮间的分割线,布局用
NSString * a;//接收输入的数字,进行拼接用
NSString *b;//接收输入的小数点“.”,控制数字的小数点的个数用
NSMutableString * c;//计算器有删除数字功能,c来接收数字的删除结果
NSString *result;//计算(+ - * /)的结果
}
@end
~~~
~~~
@implementation MainViewController
- (void)viewDidLoad {
[superviewDidLoad];
[selfcreatnav];
[selfMainview];
}
-(void)creatnav{//导航定制。
self.navigationItem.title=@"计算器";
self.view.backgroundColor=[UIColorcolorWithRed:0.738green:0.716blue:0.738alpha:1.000];
[self.navigationController.navigationBarsetTitleTextAttributes:
@{NSFontAttributeName:[UIFontsystemFontOfSize:19],
NSForegroundColorAttributeName:[UIColorwhiteColor]}];
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"title"]forBarMetrics:UIBarMetricsDefault];
}
-(void)Mainview{
WS(ws);//ws是指我用了一个宏来获取屏幕的view
NSArray * num=@[@"AC",@"exit",@"<-",@"÷",@"7",@"8",@"9",@"*",@"4",@"5",@"6",@"-",@"1",@"2",@"3",@"+",@"",@"",@".",@"="];
//下面我是用了masonry来进行界面布局,关于masonry的使用请百度或者github,这个东西来布局整个界面我只用了四十行代码,不得不说,很好很强大。
utf=[[UITextField alloc]init];
utf.backgroundColor=[UIColorcolorWithRed:0.222green:0.216blue:0.223alpha:1.000];
utf.textColor=[UIColor whiteColor];
utf.font=[UIFontsystemFontOfSize:40];
utf.userInteractionEnabled=NO;
utf.textAlignment=NSTextAlignmentRight;
[self.view addSubview:utf];
[utf mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(ws.view.mas_top).with.offset(64);
make.size.mas_equalTo(CGSizeMake(S_x,100));//s_x以及s_y是当前屏幕的宽和高,定义为宏
make.left.and.right.equalTo(ws.view);
}];
for (int i=0; i<20; i++) {
btn=[UIButton buttonWithType:UIButtonTypeCustom];
btn.backgroundColor=[UIColor clearColor];
[btn setTitle:num[i] forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:@"hehe"]forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:@"hehep"]forState:UIControlStateHighlighted];
[btn setTitleColor:[UIColorblackColor]forState:UIControlStateNormal];
[btn addTarget:selfaction:@selector(numadd:)forControlEvents:UIControlEventTouchUpInside];
btn.tag=i;
[self.view addSubview:btn];
[btn mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake((S_x-3)/4, (S_y-64-100)/5));
make.top.mas_equalTo((64+100)+(i/4)+(i/4)*(S_y-64-100)/5);
make.left.mas_equalTo(((S_x-3)/4)*(i%4)+(i%4));
//因为整个按钮都是固定大小,而数字0我希望他是大小为2个普通按钮的大小,所以我把btn。tag=16和17两个按钮隐藏,此处覆盖上定制的btn1的title为数字0的按钮
if (i==17 || i==16) {
btn.hidden=YES;
}
}];
btn1=[UIButton buttonWithType:UIButtonTypeCustom];
[btn1 setTitle:@"0"forState:UIControlStateNormal];
[btn1 setTitleColor:[UIColorblackColor]forState:UIControlStateNormal];
btn1.backgroundColor=[UIColorclearColor];
[btn1 setBackgroundImage:[UIImage imageNamed:@"hehe"]forState:UIControlStateNormal];
[btn1 setBackgroundImage:[UIImage imageNamed:@"hehep"]forState:UIControlStateHighlighted];
btn1.tag=21;
[btn1 addTarget:selfaction:@selector(numadd:)forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn1];
[btn1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(ws.view);
make.size.mas_equalTo(CGSizeMake(((S_x-3)/4)*2+1,(S_y-64-100)/5));
make.top.mas_equalTo(64+100+4+4*(S_y-64-100)/5);
}];
}
}
//到此处,布局部分结束,下面上按钮点击事件部分,点击按钮时。
-(void)numadd:(UIButton *)numpad{
switch (numpad.tag) {
//这些case是数字按钮。统一规定点击数字时utf.text进行数字拼接
case4:
case5:
case6:
case8:
case9:
case10:
case12:
case13:
case14:
case18:
case21:
//归零后继续输入数字,取消最前的0的显示
if ([utf.text isEqual:@"0"]) {
utf.text=@"";
}
//下面的两行代码 可以使输入数字拼接,但是需要注意,如果计算器有归零功能,点击之后数字归零,再继续输入数字会接着0输出,类似,189归零是0,接着输入213会成为0213,此时需要将最前面的0消失,所以有上面的那一行代码
a=[utf.text stringByAppendingString:numpad.currentTitle];
utf.text=a;
break;
case0://AC归零
utf.text=@"0";
break;
case1:
exit(0);
break;
case2:
//删除数字时。将utf。text转化为可变字符串从最后一个开始删除,设置长度为0时给text赋值为0否则崩溃
c=[NSMutableString stringWithString:utf.text];
[c deleteCharactersInRange:NSMakeRange(c.length-1,1)];
utf.text=c;
if (utf.text.length==0) {
utf.text=@"0";
}
break;
break;
default:
break;
}
//控制输入数字时“.”的出现次数为1次。检测到点的个数超过一个时,删除掉最后一个
b=@".";
NSArray * array=[acomponentsSeparatedByString:b];
NSInteger count=[arraycount]-1;
if (count>1) {
c=[NSMutableString stringWithString:utf.text];
[c deleteCharactersInRange:NSMakeRange((c.length-1),1)];
utf.text=c;
}
/**
* 该处,输入运算符时先保存之前的utf.text和此运算符,点此运算符后将text归零,接收运算符之后的数字
*/
if ([numpad.currentTitle isEqualToString:@"+"] || [numpad.currentTitle isEqualToString:@"-"] || [numpad.currentTitle isEqualToString:@"*"] || [numpad.currentTitle isEqualToString:@"÷"]) {
NSLog(@"前一组数字:%@",utf.text);
if ([utf.textisEqualToString:@""]) {
utf.text=@"0";
}
[[NSUserDefaults standardUserDefaults]setObject:utf.textforKey:@"first"];
utf.text=@"0";
NSString * symbol=[[NSUserDefaults standardUserDefaults]objectForKey:@"symbol"];
if (![symbol isEqualToString:@""]) {
symbol=@"";
}
[[NSUserDefaults standardUserDefaults]setObject:numpad.currentTitle forKey:@"symbol"];
}
if ([numpad.currentTitle isEqualToString:@"="]) {
NSString * storage=[[NSUserDefaults standardUserDefaults]objectForKey:@"first"];
NSString * symbol=[[NSUserDefaults standardUserDefaults]objectForKey:@"symbol"];
float houmian=[utf.text floatValue];
NSLog(@"后一组数字:%f",houmian);
float qianmian=[storage floatValue];
if ([symbol isEqualToString:@"+"]) {
result=[NSString stringWithFormat:@"%.2f",[self jiafa:qianmian b:houmian]];
utf.text=result;
}
if ([symbol isEqualToString:@"-"]) {
result=[NSString stringWithFormat:@"%.2f",[self jianfa:qianmian b:houmian]];
utf.text=result;
}
if ([symbol isEqualToString:@"*"]) {
result=[NSString stringWithFormat:@"%.2f",[self chengfa:qianmian b:houmian]];
utf.text=result;
}
if ([symbol isEqualToString:@"÷"]) {
result=[NSString stringWithFormat:@"%.2f",[self chufa:qianmian b:houmian]];
utf.text=result;
}
}
}
-(float)jiafa:(float)a b:(float)b{
return a+b;
}
-(float)jianfa:(float)a b:(float)b{
return a-b;
}
-(float)chengfa:(float)a b:(float)b{
return a*b;
}
-(float)chufa:(float)a b:(float)b{
return a/b;
}
~~~
//以上这些,可以实现基本的加减乘除运算,并且小数点只能输入一个,有退格和归零功能,在网上查看了其他一些大神的代码,感觉不是很容易懂,作为新手,我能做的只有这些了。。。希望对大家有所帮助,整个代码加起来也就200行不到的样子,其中masonry的应用可能有的人不太熟悉,建议花时间去了解一下。另外对按钮点击事件的判断处肯定有没想到的地方和没做好的逻辑处理,毕竟是一个简易的demo。拿过去复制粘贴就能运行了。记得先装第三方的masonry包哦!![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-03-22_56f10e2537fad.jpg)
这个是效果图,不喜欢的同学可以将按钮背景图片去掉,就可以看到masonry编辑界面的效果了,记得去掉背景图片后将self。view的backgroundcolor设置颜色,这样看的效果才会明显,类似下面这样的![](https://docs.gechiui.com/gc-content/uploads/sites/kancloud/2016-03-22_56f10e255f6ef.jpg)