Skip to content

Instantly share code, notes, and snippets.

@yusufozgul
Last active July 10, 2019 13:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yusufozgul/c93868d288b5e10faa1f4ba579d50002 to your computer and use it in GitHub Desktop.
Save yusufozgul/c93868d288b5e10faa1f4ba579d50002 to your computer and use it in GitHub Desktop.
Calculator-SwiftUI-2
var buttonsText = ["AC", "±", "%", "÷",
"7", "8", "9", "x",
"4", "5", "6", "-",
"1", "2", "3", "+",
"0", "00", ".", "="]
@State var screenValue = "0"
var calculator = Calculator()
var body: some View {
VStack { // Vertical Stack İçerisindeki elemanları yatay olarak alt alta ekler
Spacer()
VStack(alignment: .trailing) {
Text(screenValue) // Hesap Makinemizin sonuç değeri.
.bold()
.font(.largeTitle)
.padding()
.gesture(
DragGesture(minimumDistance: 50)
.onEnded { _ in
self.screenValue = self.calculator.deleteLast()
}
)
Divider()
}
ForEach((0...4)) { firstIndex in
HStack(alignment: .bottom) // Horizontal Stack İçerisindeki elemanları dikey olarak yan yana sıralar.
{
ForEach((0...3)) { secondIndex in
ZStack { // Z Satck içerisindeki elemanlar üst üste gelecek şekilde yerleştirir.
Circle()
.frame(width: 75, height: 75, alignment: .center)
.foregroundColor(self.getColor(firstIndex: firstIndex, secondIndex: secondIndex))
.padding(10)
.tapAction {
self.touchButton(button: self.buttonsText[(firstIndex * 4) + secondIndex])
}
Text(self.buttonsText[(firstIndex * 4) + secondIndex])
// .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
.color(.white)
.bold()
.font(.title)
.tapAction { // :(
self.touchButton(button: self.buttonsText[(firstIndex * 4) + secondIndex])
}
}
}
}
}
}
}
func getColor(firstIndex: Int, secondIndex: Int) -> Color
{
if firstIndex == 0 && secondIndex != 3
{ return .init(red: 0.65, green: 0.65, blue: 0.65) }
if secondIndex == 3
{ return .orange }
return .init(red: 0.20, green: 0.20, blue: 0.20)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment