ArdaReis00
Decapat
Daha fazla
- Cinsiyet
- Erkek
Merhaba, Xcode 15.4 ve swiftuı ile Apple Watch için bir klavye uygulaması geliştiriyorum. Ancak tuşların ekranda düzgün şekilde ortalanmadığı bir durumla karşılaştım. Bu konuda çözüm arayan veya benzer bir durumla karşılaşan var mı? Görüşlerinizi paylaşabilirsiniz.
Swift:
//
// ContentView.swift
// iWatchKeyboard Watch App
//
// Created by Arda Danahaliloglu on 31.08.2024.
//
import SwiftUI
struct ContentView: View {
let letters = [
["Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P"],
["A", "S", "D", "F", "G", "H", "J", "K", "L", "Ş"],
["Z", "X", "C", "V", "B", "N", "M", "Ö", "Ç"]
]
let numbersAndSymbols = [
["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"],
["-", "/", ":", ";", "(", ")", "$", "&", "@", "\""],
[".", ",", "?", "!", "'", " "]
]
@State private var inputText = ""
@State private var showingLetters = true
@State private var isShiftPressed = false
@State private var isCapsLockOn = false
@State private var shiftClickCount = 0
var body: some View {
GeometryReader { geometry in
VStack(spacing: 8) {
// İlk Satır
HStack(spacing: 4) {
ForEach(showingLetters ? letters[0] : numbersAndSymbols[0], id: \.self) { letter in
Button(action: {
inputText += formattedLetter(letter)
if isShiftPressed && !isCapsLockOn {
isShiftPressed = false
}
}) {
Text(formattedLetter(letter))
.frame(width: geometry.size.width / 12, height: geometry.size.width / 12)
.background(Color.gray)
.foregroundColor(.white)
.cornerRadius(4)
}
.buttonStyle(PlainButtonStyle())
}
}
.frame(maxWidth: .infinity, alignment: .center) // HStack hizalama
// İkinci Satır
HStack(spacing: 4) {
Spacer(minLength: geometry.size.width / 24) // Kenardan biraz boşluk
ForEach(showingLetters ? letters[1] : numbersAndSymbols[1], id: \.self) { letter in
Button(action: {
inputText += formattedLetter(letter)
if isShiftPressed && !isCapsLockOn {
isShiftPressed = false
}
}) {
Text(formattedLetter(letter))
.frame(width: geometry.size.width / 12, height: geometry.size.width / 12)
.background(Color.gray)
.foregroundColor(.white)
.cornerRadius(4)
}
.buttonStyle(PlainButtonStyle())
}
Spacer(minLength: geometry.size.width / 24)
}
.frame(maxWidth: .infinity, alignment: .center)
// Üçüncü Satır
HStack(spacing: 4) {
// Sol Shift tuşu
Button(action: {
shiftClickCount += 1
if shiftClickCount == 1 {
isShiftPressed = true
} else if shiftClickCount == 2 {
isCapsLockOn.toggle()
isShiftPressed = isCapsLockOn
shiftClickCount = 0
}
}) {
Text("⇧")
.frame(width: geometry.size.width / 12, height: geometry.size.width / 12)
.background(isCapsLockOn || isShiftPressed ? Color.blue : Color.gray)
.foregroundColor(.white)
.cornerRadius(4)
}
.buttonStyle(PlainButtonStyle())
Spacer(minLength: geometry.size.width / 36) // Daha küçük kenar boşluğu
ForEach(showingLetters ? letters[2] : numbersAndSymbols[2], id: \.self) { letter in
Button(action: {
inputText += formattedLetter(letter)
if isShiftPressed && !isCapsLockOn {
isShiftPressed = false
}
}) {
Text(formattedLetter(letter))
.frame(width: geometry.size.width / 12, height: geometry.size.width / 12)
.background(Color.gray)
.foregroundColor(.white)
.cornerRadius(4)
}
.buttonStyle(PlainButtonStyle())
}
Spacer(minLength: geometry.size.width / 36)
// Sağ Shift tuşu
Button(action: {
shiftClickCount += 1
if shiftClickCount == 1 {
isShiftPressed = true
} else if shiftClickCount == 2 {
isCapsLockOn.toggle()
isShiftPressed = isCapsLockOn
shiftClickCount = 0
}
}) {
Text("⇧")
.frame(width: geometry.size.width / 12, height: geometry.size.width / 12)
.background(isCapsLockOn || isShiftPressed ? Color.blue : Color.gray)
.foregroundColor(.white)
.cornerRadius(4)
}
.buttonStyle(PlainButtonStyle())
}
.frame(maxWidth: .infinity, alignment: .center)
// Dördüncü Satır (Space Tuşu)
HStack {
Spacer()
Button(action: {
inputText += " " // Boşluk ekle
}) {
Text("Space")
.frame(width: geometry.size.width / 2.2, height: geometry.size.width / 12)
.background(Color.gray)
.foregroundColor(.white)
.cornerRadius(4)
}
.buttonStyle(PlainButtonStyle())
Spacer()
}
.frame(maxWidth: .infinity, alignment: .center)
}
.padding(.horizontal, 2) // Dış çerçevede biraz padding
}
}
func formattedLetter(_ letter: String) -> String {
if isCapsLockOn || isShiftPressed {
return letter.uppercased()
} else {
return letter.lowercased()
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
.previewDevice("Apple Watch Series 9 - 45mm")
}
}