Skip to content

Commit

Permalink
Merge pull request #42 from hdcola/hdcola
Browse files Browse the repository at this point in the history
add keyboardShortcuts Tap
  • Loading branch information
hdcola authored Sep 12, 2022
2 parents 56b4871 + a936856 commit cffb7da
Show file tree
Hide file tree
Showing 2 changed files with 191 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
//
// SwiftUIView.swift
//
//
// Created by 老房东 on 2022-09-12.
//

import SwiftUI

struct KeyboardShortcutsControlView: View {

var body: some View {
ScrollView{
VStack{
HeadlineView(
title: "Keyboard Shortcuts",
url: "https://developer.apple.com/documentation/swiftui/view/keyboardshortcut(_:)-8liec",
description: "Assigns a keyboard shortcut to the modified control."
)
KeyboardShortcutsSampleView()
Divider()
GameControlSampleView()
}
.padding()
}
}
}

private struct GameControlSampleView: View {
var code = """
struct GameControlSampleView: View {
@State var x = 0.0
@State var y = 0.0
@State var degress = 0.0
var body: some View {
VStack{
ZStack{
Rectangle()
.fill(.cyan)
.frame(width: 50,height: 50)
.offset(x:x,y:y)
.rotationEffect(.degrees(degress))
}
.frame(width: 300,height: 300)
.border(.black)
HStack{
Button("⬅️"){
withAnimation {
x -= 5
}
}.keyboardShortcut(.leftArrow, modifiers: [])
Button("➡️"){
withAnimation {
x += 5
}
}.keyboardShortcut(.rightArrow, modifiers: [])
Button("⬆️"){
withAnimation {
y -= 5
}
}.keyboardShortcut(.upArrow, modifiers: [])
Button("⬇️"){
withAnimation {
y += 5
}
}.keyboardShortcut(.downArrow, modifiers: [])
Button("Space🔄"){
withAnimation {
degress -= 10
}
}.keyboardShortcut(.space, modifiers: [])
Button("Enter⏹"){
withAnimation {
x = 0
y = 0
degress = 0
}
}.keyboardShortcut(.return, modifiers: [])
}
}
}
}
"""
@State var x = 0.0
@State var y = 0.0
@State var degress = 0.0
var body: some View {
VStack{
Text("Move Game")
.font(.title2)
CodePreviewView(code: code)
ZStack{
Rectangle()
.fill(.cyan)
.frame(width: 50,height: 50)
.offset(x:x,y:y)
.rotationEffect(.degrees(degress))
}
.frame(width: 300,height: 300)
.border(.black)
HStack{
Button("⬅️"){
withAnimation {
x -= 5
}
}.keyboardShortcut(.leftArrow, modifiers: [])
Button("➡️"){
withAnimation {
x += 5
}
}.keyboardShortcut(.rightArrow, modifiers: [])
Button("⬆️"){
withAnimation {
y -= 5
}
}.keyboardShortcut(.upArrow, modifiers: [])
Button("⬇️"){
withAnimation {
y += 5
}
}.keyboardShortcut(.downArrow, modifiers: [])
Button("Space🔄"){
withAnimation {
degress -= 10
}
}.keyboardShortcut(.space, modifiers: [])
Button("Enter⏹"){
withAnimation {
x = 0
y = 0
degress = 0
}
}.keyboardShortcut(.return, modifiers: [])
}
}
}
}

private struct KeyboardShortcutsSampleView: View {
var code = """
Button("a"){
inputKey = "a"
}.keyboardShortcut("A",modifiers: [])
Button("B"){
inputKey = "b"
}.keyboardShortcut("B",modifiers: [.shift])
Button("b"){
inputKey = "b"
}.keyboardShortcut("B",modifiers: [])
Button("c"){
inputKey = "c"
}
.hidden()
.keyboardShortcut("c",modifiers: [])
"""
@State var inputKey = ""
var body: some View {
VStack{
CodePreviewView(code: code)
Text("Input :\(inputKey)")
HStack{
Button("a"){
inputKey = "a"
}.keyboardShortcut("A",modifiers: [])
Button("B"){
inputKey = "B"
}.keyboardShortcut("B",modifiers: [.shift])
Button("b"){
inputKey = "b"
}.keyboardShortcut("B",modifiers: [])
Button("c"){
inputKey = "c"
}
.hidden()
.keyboardShortcut("c",modifiers: [])
}
}
}
}

struct KeyboardShortcutsControlView_Previews: PreviewProvider {
static var previews: some View {
KeyboardShortcutsControlView()
}
}
6 changes: 6 additions & 0 deletions OneTapSwiftUI.swiftpm/Sidebar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ struct Sidebar: View {
}
}

Section("Input and event modifiers"){
NavigationLink("Keyboard shortcuts"){
KeyboardShortcutsControlView()
}
}

Section("Map"){
NavigationLink("Map"){
MapControlView()
Expand Down

0 comments on commit cffb7da

Please sign in to comment.