首页 > 代码库 > UI 纯代码实现计算器
UI 纯代码实现计算器
// MHTAppDelegate.h
// TestCa
// Copyright (c) 2014年 Summer. All rights reserved.
#import<UIKit/UIKit.h>
@interface MHTAppDelegate :UIResponder <UIApplicationDelegate,UITextFieldDelegate>
@property (retain,nonatomic) UIWindow *window;
@end
// MHTAppDelegate.m
// TestCal
// Copyright (c) 2014年 Summer. All rights reserved.
#import"MHTAppDelegate.h"
@interfaceMHTAppDelegate()
{
UIView *_containerView;
UIButton *_desBut1;
UIButton *_desBut2;
UIButton *_desBut3;
UIButton *_desBut4;
UILabel *_hintLine;
UITextField *_textField;
BOOL _isHave;
BOOL _isResult;
}
@property (retain,nonatomic) NSString *_str;
@property (retain,nonatomic) NSString *_number;
@end
@implementation MHTAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindowalloc] initWithFrame:[[UIScreenmainScreen] bounds]];
// Override point for customization after application launch.
//创建底层的背景视图
_containerView = [[UIViewalloc] initWithFrame:CGRectMake(0,0, 320,568)];
_containerView.backgroundColor = [UIColorwhiteColor];
[self.windowaddSubview:_containerView];
[_containerViewrelease];
//baseCalculate
[selfbaseCalculate];
//Line
[selfline];
_isHave = NO;
_isResult = NO;
self.window.backgroundColor = [UIColorwhiteColor];
[self.windowmakeKeyAndVisible];
return YES;
}
- (void)baseCalculate
{int j =0;
NSArray *calArr =@[@"C",@"+/-", @"%", @"÷",@"7", @"8", @"9",@"x", @"4", @"5",@"6", @"-", @"1",@"2", @"3", @"+",@"0", @".", @"="];
NSInteger n = 0;
for (int i = 0; i <5; i++) {
for ( j = 0; j < 4; j++) {
if (i == 4 && j == 0) {
_desBut1 = [UIButtonbuttonWithType:UIButtonTypeSystem];
_desBut1.frame = CGRectMake(0, 488, 160, 80);
[_desBut1setTitle:calArr[n] forState:UIControlStateNormal];
[_desBut1setTitleColor:[UIColorblackColor] forState:UIControlStateNormal];
[_desBut1addTarget:selfaction:@selector(numberClick:)forControlEvents:UIControlEventTouchUpInside];
_desBut1.titleLabel.font = [UIFontfontWithName:@"AppleSDGothicNeo-Thin"size:30];
_desBut1.backgroundColor = [UIColorwhiteColor];
[_containerViewaddSubview:_desBut1];
j =2;
n++;
}
_desBut1 = [UIButtonbuttonWithType:UIButtonTypeSystem];
_desBut1.frame = CGRectMake(0 + j * 80, 168 + 80 * i, 80, 80);
[_desBut1setTitle:calArr[n] forState:UIControlStateNormal];
[_desBut1setTitleColor:[UIColorblackColor] forState:UIControlStateNormal];
if (j == 3) {
_desBut1.backgroundColor = [UIColororangeColor];
_desBut1.titleLabel.font = [UIFontfontWithName:@"AppleSDGothicNeo-Thin"size:30];
}elseif (i == 0){
_desBut1.backgroundColor = [UIColorlightGrayColor];
}else
{
_desBut1.backgroundColor = [UIColorwhiteColor];
}
_desBut1.titleLabel.font = [UIFontfontWithName:@"AppleSDGothicNeo-Thin"size:30];
[_containerViewaddSubview:_desBut1];
if (n == 4 || n == 5 || n == 6 || n == 8 || n ==9 || n == 10 || n == 12 || n == 13 || n == 14 || n ==17 || n == 16) {
[_desBut1addTarget:selfaction:@selector(numberClick:)forControlEvents:UIControlEventTouchUpInside];
}
if (n == 0) {
[_desBut1addTarget:selfaction:@selector(clearClick:)forControlEvents:UIControlEventTouchUpInside];
}
if (n == 1) {
[_desBut1addTarget:selfaction:@selector(oppositeClick:)forControlEvents:UIControlEventTouchUpInside];
}
if (n == 2) {
[_desBut1addTarget:selfaction:@selector(percentClick:)forControlEvents:UIControlEventTouchUpInside];
}
if (n == 3 || n == 7 || n == 11 || n == 15 || n ==14) {
[_desBut1addTarget:selfaction:@selector(operationClick:)forControlEvents:UIControlEventTouchUpInside];
}
if (n == 18) {
[_desBut1addTarget:selfaction:@selector(resultClick:)forControlEvents:UIControlEventTouchUpInside];
}
n++;
}
j =0;
}
NSLog(@"test:%d", n);
[selfdisplayTextField];
}
- (void)displayTextField
{
_textField = [[UITextFieldalloc] initWithFrame:CGRectMake(0,40, 320,128)];
_textField.backgroundColor = [UIColorblackColor];
_textField.text =@"0";
//不可写
_textField.enabled =NO;
_textField.delegate =self;
_textField.textColor = [UIColorwhiteColor];
_textField.font = [UIFontfontWithName:@"AppleSDGothicNeo-Thin"size:40];
_textField.textAlignment =NSTextAlignmentRight;
[_containerViewaddSubview:_textField];
[_textFieldrelease];
}
- (void)oppositeClick:(UIButton *)oppositeBut
{
CGFloat num = [_textField.textfloatValue];
_textField.text = [NSStringstringWithFormat:@"%g", (-num)];
}
- (void)percentClick:(UIButton *)percentBut
{
CGFloat num = [_textField.textfloatValue];
_textField.text = [NSStringstringWithFormat:@"%g", (num / 100)];
}
- (void)clearClick:(UIButton *)clearBut
{
_textField.text =nil;
}
- (void)numberClick:(UIButton *)numBut
{
if ( _isHave || [_textField.textisEqualToString:@"0"] ||_isResult ) {
_textField.text = @"";
_textField.text = [_textField.textstringByAppendingString:numBut.currentTitle];
}else{
_textField.text = [_textField.textstringByAppendingString:numBut.currentTitle];
}
_isHave = NO;
_isResult = NO;
}
- (void)operationClick:(UIButton *)operationBut
{
if ([operationBut.currentTitle isEqualToString:@"+"]) {
self._str = [_textField.textstringByAppendingString:@"+"];
_isHave = YES;
}else if ([operationBut.currentTitleisEqualToString:@"-"]) {
self._str = [_textField.textstringByAppendingString:@"-"];
_isHave = YES;
}else if ([operationBut.currentTitleisEqualToString:@"x"]) {
self._str = [_textField.textstringByAppendingString:@"x"];
_isHave = YES;
}elseif ([operationBut.currentTitle isEqualToString:@"÷"]) {
self._str = [_textField.textstringByAppendingString:@"÷"];
_isHave = YES;
}
}
- (void)resultClick:(UIButton *)resultBut
{
NSLog(@"aa");
if ([self._strhasSuffix:@"+"]) {
CGFloat value1 = [self._strfloatValue];
CGFloat value2 = [_textField.textfloatValue];
_textField.text = [NSStringstringWithFormat:@"%g",(value1 + value2)];
self._str = @"";
_isResult = YES;
}elseif ([self._strhasSuffix:@"-"]){
CGFloat value1 = [self._strfloatValue];
CGFloat value2 = [_textField.textfloatValue];
_textField.text = [NSStringstringWithFormat:@"%g",(value1 - value2)];
self._str = @"";
_isResult = YES;
}elseif([self._strhasSuffix:@"x"]){
CGFloat value1 = [self._strfloatValue];
CGFloat value2 = [_textField.textfloatValue];
_textField.text = [NSStringstringWithFormat:@"%g",(value1 * value2)];
self._str = @"";
_isResult = YES;
}else if([self._strhasSuffix:@"÷"]){
CGFloat value1 = [self._strfloatValue];
CGFloat value2 = [_textField.textfloatValue];
_textField.text = [NSStringstringWithFormat:@"%g",(value1 / value2)];
self._str = @"";
_isResult = YES;
}else{
self._str = @"";
_isResult = YES;
}
}
//收回键盘
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
return YES;
}
// Line
- (void)line
{
//Line
for (int i = 0; i <4; i++) {
UILabel *hintLine = [[UILabel alloc] initWithFrame:CGRectMake(0,248 + 80 * i, 320,0.5)];
hintLine.backgroundColor = [UIColorblackColor];
[_containerViewaddSubview:hintLine];
[hintLinerelease];
}
//Line
for (int i = 0; i <3; i++) {
if (i == 0) {
_hintLine = [[UILabel alloc] initWithFrame:CGRectMake(80,168, 0.5, 320)];
_hintLine.backgroundColor = [UIColorblackColor];
[_containerViewaddSubview:_hintLine];
[_hintLinerelease];
}else{
_hintLine = [[UILabel alloc] initWithFrame:CGRectMake(80 + i *80, 168, 0.5,400)];
_hintLine.backgroundColor = [UIColorblackColor];
[_containerViewaddSubview:_hintLine];
[_hintLinerelease];
}
}
}
- (void)applicationWillResignActive:(UIApplication *)application
{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
- (void)dealloc
{
[_windowrelease];
[superdealloc];
}
@end
UI 纯代码实现计算器