我是新手为iOS创建应用程序。最近,我一直致力于根据手势制作计算器应用程序。我正在使用switch case来改变操作,我最初使用按钮操作它,但现在我想使用手势。到目前为止,这是我的代码。 opAddition函数是我尝试它的第一种方式,但它不会切换函数。例如,如果我想从加法切换到减法,那么它只能在输入数字后才能工作。这就是我尝试使用Switch-Case的原因,但现在我不知道如何将手势链接到它们。
@implementation ABViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
calculatorScreen.adjustsFontSizeToFitWidth = YES;
self.notification.layer.cornerRadius = 90;
self.notification.layer.masksToBounds = YES;
self.notification.alpha = 0;
[[self.notification layer] setBorderWidth:8.0f];
[[self.notification layer] setBorderColor:[UIColor whiteColor].CGColor];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - buttons
-(IBAction)buttonDigitPressed:(id)sender{
currentNumber = currentNumber *10 + (int)[sender tag];
NSString *number = [NSString stringWithFormat:@"%.1f", currentNumber];
if ( number.length < 11) {
calculatorScreen.text = number;
}
};
#pragma mark - operator button code
-(IBAction)buttonOperationPressed:(id)sender{
if (currentOperation == 0) { result = currentOperation; }
else {
if (_operationSimple.direction == UISwipeGestureRecognizerDirectionUp){
currentOperation = 1;
} else if (_operationSimple.direction == UISwipeGestureRecognizerDirectionDown){
currentOperation = 2;
}
switch (currentOperation) {
case 1:
result = result + currentNumber;
self.notificationText.text = @"+";
[self notify];
break;
case 2:
result = result - currentNumber;
self.notificationText.text = @"-";
[self notify];
break;
case 3:
result = result * currentNumber;
break;
case 4:
result = result / currentNumber;
break;
case 5:
currentOperation = 0;
break;
}
}
currentNumber = 0;
calculatorScreen.text = [NSString stringWithFormat:@"%.1f", result];
if ( [sender tag] == 0) { result = 0; }
currentNumber = [sender tag];
};
#pragma mark - Cancelations
-(IBAction)cancelInput{
currentNumber = 0;
self.notificationText.text = @"C";
[self notify];
calculatorScreen.text = @"0.0";
result = 0.0;
NSLog(@"Cancel Input");
}
-(IBAction)cancelOperation{
currentNumber = 0;
self.notificationText.text = @"AC";
[self notify];
calculatorScreen.text = @"0.0";
currentOperation = 0;
NSLog(@"Clear Operation");
}
double newresult = 0;
#pragma mark - Operations
-(IBAction)opAddition{
result = result + currentNumber;
self.notificationText.text = @"+";
[self notify];
newresult = result;
currentNumber = 0;
NSLog(@"Number Added. New result:");
NSLog([NSString stringWithFormat:@"%2f", newresult]);
calculatorScreen.text = [NSString stringWithFormat:@"%.1f", result];
}
#pragma mark - notification stuffs
- (void)notify{
[UIView animateWithDuration:1.0 animations:^{
self.notification.alpha = 1.0f;
}];
[UIView animateWithDuration:1.0 animations:^{
self.notification.alpha = 0.0f;
}];
}
@end