iOS用のアプリを作成するのは初めてです。最近は、ジェスチャーを使った電卓アプリの制作に取り組んでいます。スイッチケースを使って操作を変えているのですが、元々はボタン付きのリグだったのですが、ジェスチャーを使いたいと思っています。これまでの私のコードは次のとおりです。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