This repository was archived by the owner on Jul 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSASViewController.m
199 lines (165 loc) · 5.51 KB
/
SASViewController.m
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
//
// SASViewController.m
// Hesap Makinesi
//
// Created by Şakir Şensoy on 16/06/14.
// Copyright (c) 2014 Şakir Şensoy. All rights reserved.
//
#import "SASViewController.h"
@interface SASViewController ()
@end
@implementation SASViewController
// view yüklenirken
- (void)viewDidLoad
{
// parent objedeki aynı metodu çalıştır
[super viewDidLoad];
// calculator objemizin bir instance'ını calculator
// property'mize atayalım
self.calculator = [[SASCalculator alloc] init];
// varsayılan değerleri atayalım
self.number1 = self.number2 = @"";
self.screen.text = @"0";
// ekran (sayıları gösteren label) font büyüklüğümüz
// taşma yaşanmaması için ekran genişliğine göre optimize
// edilsin
self.screen.adjustsFontSizeToFitWidth = YES;
// ses dosyamızın URL'ini bulalım
NSString *soundFilePath = [NSString
stringWithFormat:@"%@/sound_effect.mp3",
[[NSBundle mainBundle] resourcePath]];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
// player'ı aktif hale getir
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
// sayılara basılırsa
- (IBAction)numberButtonPressed:(id)sender
{
UIButton *button = (UIButton *) sender;
// ekrandaki sıfırı sil
if ([self.screen.text isEqual:@"0"])
{
self.screen.text = @"";
}
// işlem operatörü atanmadıysa gelen değeri sayı1'e ata
if (self.calculator.process == nil)
{
self.number1 = [self.number1 stringByAppendingString: button.currentTitle];
}
// işlem operatörü atandıysa gelen değeri sayı2'ye ata
else
{
self.number2 = [self.number2 stringByAppendingString: button.currentTitle];
}
// ekranı yenile
[self screenOrganize];
}
// işlem operatörlerine basılırsa
- (IBAction)processButtonPressed:(id)sender
{
// sender değişkenini UIButton'a cast ederek
// basılan button'a erişelim
UIButton *button = (UIButton *) sender;
// sayı1 boş değilse operatör atamasını gerçekleştirelim
if (! [self.number1 isEqual:@""])
{
// basılan butonumuzun title'ı aslında operatörümüz
// currentTitle ile operatörü alıyor ve calculator objemize
// gerekli atamayı yapıyoruz
self.calculator.process = button.currentTitle;
// ekranı yenile
[self screenOrganize];
}
}
// sonuç butonuna basılırsa
- (IBAction)resultButtonPressed:(id)sender
{
// sayı1, sayı2 ve işlem operatörü boş değilse
if (! [self.number1 isEqual:@""] &&
! [self.number2 isEqual:@""] &&
self.calculator.process != nil )
{
// sayı1 ve sayı2 adlı değişkenleri NSNumber'a cast ederek
// calculator objemizdeki yerlerine ata
self.calculator.number1 = (NSNumber *) self.number1;
self.calculator.number2 = (NSNumber *) self.number2;
// calculator objemizin result metodunu çalıştır ve sonucu
// string olarak ekrana bastır
self.screen.text = [[self.calculator result] stringValue];
// temizlik
self.number1 = self.number2 = @"";
[self.calculator cancel];
// bide ses efekti verelim ki, millet hesaplandığını anlasın :D
[self.player play];
}
}
// vazgeç butonuna basılırsa
- (IBAction)cancelButtonPressed:(id)sender
{
[self cancelProcess];
}
// hesap makinesini sıfırla
- (void)cancelProcess
{
// değerleri boşalt
self.number1 = self.number2 = @"";
[self.calculator cancel];
// ekranı sıfırla
self.screen.text = @"0";
}
// ekranımızı organize eden metodumuz
- (void)screenOrganize
{
NSString *screenText = @"";
// sayı 1 boş değilse
if (! [self.number1 isEqual:@""])
{
// ekrandaki yazıya ekle
screenText = [screenText stringByAppendingString: self.number1];
// işlem operatörü boş değilse
if (self.calculator.process != nil)
{
// önce alt satıra geç
screenText = [screenText stringByAppendingString: @"\n"];
// ve ekrana operatörü ekle
screenText = [screenText stringByAppendingString: self.calculator.process];
if (! [self.number2 isEqual:@""])
{
screenText = [screenText stringByAppendingString: @"\n"];
screenText = [screenText stringByAppendingString: self.number2];
}
}
}
// ekrandaki metini güncelle
self.screen.text = screenText;
}
// dokunmaları yakala
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
// sadece çift dokunmaları yakalıyoruz
if (touch.tapCount == 2)
{
// dokunulan yerin koordinatları, label'ın alanı içinde kalıyorsa
// ekranı sıfırla
if (CGRectContainsPoint([self.screen frame], [touch locationInView:self.view]))
{
[self cancelProcess];
}
}
}
// hareketleri yakala
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
// sadece çalkalama hareketini yakala ve ekranı sıfırla
if (motion == UIEventSubtypeMotionShake)
{
[self cancelProcess];
}
}
@end