Spritekit
My WordPress site
My WordPress site
swiftuiカテゴリー記事の一覧です
import SwiftUI
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate, ObservableObject { // <<: Here 1
// @Published var updates = 0
@Published var gameScore = 0 // <<: Here 2
@Published var lenght: CGFloat = 1
var up: Bool = false
// let removeLabel = SKAction.sequence([SKAction.fadeIn(withDuration: 0.3), SKAction.wait(forDuration: 0.8), SKAction.fadeOut(withDuration: 0.3)])
let tball = SKShapeNode(circleOfRadius: 20)
var lastUpdateTime2 : TimeInterval = 0
override func didMove(to view: SKView) {
backgroundColor = .yellow
physicsBody = SKPhysicsBody(edgeLoopFrom: frame)
tball.position = CGPoint(x: 0, y: 0)
tball.fillColor = .blue
self.addChild(tball)
// tball.zPosition = 10
}
func change(){
if self.lenght > 100 {
up = false
}else if (self.lenght < 10){
up = true
}
up ? (self.lenght += 1) : (self.lenght -= 1)
}
override func update(_ currentTime: TimeInterval) {
// self.change()
if lastUpdateTime2 == 0 {
lastUpdateTime2 = currentTime
}
if currentTime - lastUpdateTime2 > 1 - 0.9 {
self.change()
lastUpdateTime2 = currentTime
}
}
/*
func ball() {
let ball = SKShapeNode(circleOfRadius: 20)
let MinValue = self.size.width / 8
let MaxValue = self.size.width - 50
let SpawnPoint = UInt32(MaxValue - MinValue)
ball.position = CGPoint(x: CGFloat(arc4random_uniform(SpawnPoint)), y: 200)
ball.fillColor = .blue
self.addChild(ball)
}
*/
func ball() {
let moveDown = SKAction.moveTo(x:130, duration: 2.5)
let moveUp = SKAction.moveTo(x: -130, duration: 2.5)
let actionSequence2 = SKAction.sequence([moveDown, moveUp])
let moveRepeat = SKAction.repeatForever(actionSequence2)
tball.run(moveRepeat, withKey: "moveX")
}
func ball2() {
let moveDown2 = SKAction.moveTo(x:130, duration: -2.5)
let moveUp2 = SKAction.moveTo(x: -130, duration: -2.5)
let actionSequence3 = SKAction.sequence([moveDown2, moveUp2])
let moveRepeat2 = SKAction.repeatForever(actionSequence3)
tball.run(moveRepeat2, withKey: "moveX2")
}
func addScore(){
if gameScore < 10 {
gameScore += 1
} else {
gameScore += 5
}
}
}
struct ContentView: View {
@State var flag = true
@State var showingSheet : Bool = false
// @StateObject var gameScene = GameScene() // <<: Here 3
/*
let scene: GameScene = {
let scene = GameScene()
scene.size = CGSize(width: 300, height: 300)
scene.scaleMode = .fill
return scene
}()
*/
@StateObject private var scene: GameScene = {
let scene = GameScene()
scene.size = CGSize(width: 300, height: 300)
scene.anchorPoint = CGPoint(x: 0.5, y: 0.5)
scene.scaleMode = .fill
return scene
}()
var body: some View {
Spacer()
VStack {
Button(action: {
if(self.flag){
scene.ball()
self.flag = false
}
else{
scene.ball2()
self.flag = true
}
}
){
Text("ball start on off")
.font(.title3)
.foregroundColor(Color.purple)
.padding(.bottom, 20)
}
Button(
action: { showingSheet = true },
label: {
Text("ShowSheet")
})
.sheet(isPresented: $showingSheet){
SecondSheet()
}
}
ZStack {
SpriteView(scene: scene)
.frame(width: 300, height: 300)
.ignoresSafeArea()
Circle().frame(width: scene.lenght, height: scene.lenght)
.foregroundColor(.green)
.padding(.top, 150)
Button(action: {
scene.addScore()
}) {
Text("Score: \(scene.gameScore)")
.font(.title3)
.frame(width: 110, height: 30, alignment: .center)
.foregroundColor(Color.white)
.background(Color.pink)
.cornerRadius(5, antialiased: true)
.padding(.bottom, 130)
}
}
.padding(.bottom, 110)
}
}
struct SecondSheet: View {
@Environment(\.dismiss) var dismiss
var body: some View {
VStack{
Text("SecondSheet")
.fontWeight(.heavy)
.font(.title)
Button("画面を閉じる") {
dismiss()
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
import SwiftUI
struct ContentView: View {
@State var showHamburgerMenu = false
var body: some View {
let drag = DragGesture()
.onEnded {
if $0.translation.width < -100 {
withAnimation {
self.showHamburgerMenu = false
}
}
}
NavigationView {
GeometryReader { geometry in
ZStack(alignment: .leading) {
MainView(showHamburgerMenu: self.$showHamburgerMenu)
.frame(width: geometry.size.width, height: geometry.size.height)
.offset(x: self.showHamburgerMenu ? geometry.size.width/2 : 0)
.disabled(self.showHamburgerMenu ? true : false)
if self.showHamburgerMenu {
SideMenuView(isOpen: $showHamburgerMenu)
.frame(width: geometry.size.width)
// .frame(width: geometry.size.width/2)
.transition(.move(edge: .leading))
}
}
.gesture(drag)
}
.navigationTitle("← Menu")
.navigationBarTitleDisplayMode(.inline)
.navigationBarItems(leading: (
Button(action: {
withAnimation {
self.showHamburgerMenu.toggle()
}
}) {
// Image(systemName: "line.horizontal.3")
Image(systemName: showHamburgerMenu ? "xmark" : "line.horizontal.3")
// .imageScale(.large)
.font(.title2)
.foregroundColor(.red)
}
))
}
}
}
struct MainView: View {
@Binding var showHamburgerMenu: Bool
@State var isShowSubViw = false
var body: some View {
ZStack{
Color.yellow
.frame(maxWidth: .infinity, maxHeight: .infinity)
.ignoresSafeArea()
ScrollView(.horizontal, showsIndicators: false) {
HStack{
GeometryReader { geometry in
ImageCarouselView(numberOfImages: 1) {
// Text("Bananas 🐻🐻")
Image(decorative: "d01")
.resizable()
.scaledToFit()
.frame(maxWidth: 80, maxHeight: 80, alignment: .center)
.clipShape(Circle())
}
}.frame(width: 383, height: 240)
GeometryReader { geometry in
ImageCarouselView(numberOfImages: 1) {
Text("Peaches 🍑🍑")
}
}.frame(width: 380, height: 240)
GeometryReader { geometry in
ImageCarouselView(numberOfImages: 1) {
Text("Apples 🍏🍏")
}
}.frame(width: 380, height: 240)
GeometryReader { geometry in
ImageCarouselView(numberOfImages: 1) {
Text("inuwan 🐶🐶")
}
}.frame(width: 382, height: 240)
GeometryReader { geometry in
ImageCarouselView(numberOfImages: 1) {
// Text("sakana 🐟🐟")
Image(decorative: "d04")
.resizable()
.scaledToFit()
.frame(maxWidth: 80, maxHeight: 80, alignment: .center)
.clipShape(Circle())
}
}.frame(width: 388, height: 240)
}.padding(.leading)
}
.foregroundColor(Color.white)
// .background(Color.yellow)
}
GeometryReader { geometory in
ZStack {
if isShowSubViw {
SubView()
} else {
Button(action: {
withAnimation() {
self.isShowSubViw.toggle()
}
}) {
Text("SubViewへ")
}
.padding(.top, 100)
VStack {
Text("MainView")
.font(.system(size: 35, weight: .black, design: .default))
.padding(.top, 50)
}
}
}
.frame(width: geometory.size.width,
height: geometory.size.height)
// .background(Color.yellow)
.animation(.easeInOut(duration: 0.42))
}
.transition(.move(edge: .leading))
}
}
struct SubView: View {
@State var isShowSubViw = false
let imageUrl = URL(string: "https://kyou.gif.jp/img/d07.jpg")
var body: some View {
ZStack{
LinearGradient(gradient: Gradient(colors: [Color.yellow, Color.green]), startPoint: .init(x: 0.3, y: 0.3), endPoint: .init(x: 0.55, y: 0.55))
.frame(maxWidth: .infinity, maxHeight: .infinity)
.ignoresSafeArea()
VStack{
AsyncImage(url: imageUrl) { image in
image.resizable()
.aspectRatio(contentMode: .fill)
.clipShape(Circle())
} placeholder: {
ProgressView()
}
.frame(width: 80, height: 80, alignment: .center)
.padding(.bottom, 50)
.padding(.trailing, 5)
// .mask(RoundedRectangle(cornerRadius: 10))
}
GeometryReader { geometory in
ZStack {
if isShowSubViw {
MainView(showHamburgerMenu: $isShowSubViw)
} else {
Button(action: {
withAnimation() {
self.isShowSubViw.toggle()
}
}) {
Text("MainViewへ")
}
.padding(.top, 100)
VStack {
Text("SubView")
.font(.system(size: 35, weight: .black, design: .default))
.padding(.top, 50)
}
}
}
.frame(width: geometory.size.width,
height: geometory.size.height)
// .background(Color.green)
// .background(
// gradient: Gradient(colors: [Color.yellow, Color.green]),
// startPoint: .init(x: 0.3, y: 0.3), // start地点
// endPoint: .init(x: 0.55, y: 0.55) // end地点
// ))
.animation(.easeInOut(duration: 0.42))
}
.transition(.move(edge: .leading))
}
}
}
struct SideMenuView: View {
@Binding var isOpen: Bool
let width: CGFloat = 280
var body: some View {
ZStack {
GeometryReader { geometry in
EmptyView()
}
.background(Color.gray.opacity(0.3))
.opacity(self.isOpen ? 1.0 : 0.0)
.opacity(1.0)
.animation(.easeIn(duration: 0.25))
.onTapGesture {
self.isOpen = false
}
VStack(alignment: .leading) {
HStack {
Image(systemName: "person")
.foregroundColor(.white)
.imageScale(.large)
NavigationLink(destination: ProfileView()) {
Text("Profile")
.foregroundColor(.white)
.font(.headline)
}
}
.padding(.top, 80)
HStack {
Image(systemName: "envelope")
.foregroundColor(.white)
.imageScale(.large)
NavigationLink(destination: MessagesView()) {
Text("Messages")
.foregroundColor(.white)
.font(.headline)
}
}
.padding(.top, 30)
HStack {
Image(systemName: "gear")
.foregroundColor(.white)
.imageScale(.large)
NavigationLink(destination: SettingsView()) {
Text("Settings")
.foregroundColor(.white)
.font(.headline)
}
}
.padding(.top, 30)
HStack {
Image(systemName: "pencil.circle")
.foregroundColor(.white)
.imageScale(.large)
NavigationLink(destination: PencilView()) {
Text("Pencil test")
.foregroundColor(.white)
.font(.headline)
}
}
.padding(.top, 30)
Spacer()
}
// .padding()
// .frame(maxWidth: .infinity, alignment: .leading)
// .background(Color(red: 132/255, green: 132/255, blue: 132/255))
// .edgesIgnoringSafeArea(.all)
.frame(width: width)
// .background(Color(UIColor.systemGray6))
.background(Color.gray)
.offset(x: self.isOpen ? 0 : -self.width)
.animation(.easeIn(duration: 0.25))
// .padding(.top, 100)
.padding(.leading, -100)
Spacer()
}
}
}
struct PencilView: View {
var body: some View {
ZStack{
Color.purple
.frame(maxWidth: .infinity, maxHeight: .infinity)
.ignoresSafeArea()
VStack{
Text("PencilView Subview")
Text("MyPencilView")
.font(.title)
}
.navigationTitle("PencilView")
.navigationBarBackButtonHidden(true)
.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
NavigationBackItem(title: "Menuへ戻る")
}
}
}
}
}
struct SettingsView: View {
var body: some View {
ZStack{
Color.yellow
.frame(maxWidth: .infinity, maxHeight: .infinity)
.ignoresSafeArea()
VStack{
Text("Settings Subview")
Text("MySubview")
.font(.title)
}
.navigationTitle("SettingsView")
.navigationBarBackButtonHidden(true)
.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
NavigationBackItem(title: "Menuへ戻る")
}
}
}
}
}
struct MessagesView: View {
var body: some View {
ZStack{
Color.green
.frame(maxWidth: .infinity, maxHeight: .infinity)
.ignoresSafeArea()
VStack{
Text("Messages Subview")
}
.navigationTitle("MessagesView")
.navigationBarBackButtonHidden(true)
.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
NavigationBackItem(title: "Menuへ戻る")
}
}
}
}
}
struct ProfileView: View {
init(){
//List全体の背景色の設定
UITableView.appearance().backgroundColor = UIColor.white
}
@State private var isNextPresented = false
var body: some View {
ZStack{
Color.orange
.frame(maxWidth: .infinity, maxHeight: .infinity)
.ignoresSafeArea()
NavigationView {
VStack{
Text("Profile Subview")
.font(.title)
List {
NavigationLink("ChildViewへ") {
ChildView()
}.listRowBackground(Color.gray.opacity(0.2))
}
}
}
// .navigationTitle("ProfileView")
.navigationBarBackButtonHidden(true)
.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
NavigationBackItem(title: "Menuへ戻る")
}
ToolbarItem(placement: .navigationBarTrailing){
Button {
isNextPresented.toggle()
} label: {
HStack {
Text("Next")
Image(systemName: "person.fill")
}
}
.fullScreenCover(isPresented: $isNextPresented) {
NextView()
}
}
}
}
}
}
struct ChildView: View {
@Environment(\.dismiss) var dismiss
var body: some View {
VStack {
Text("ChildView")
.font(.title)
}
.navigationBarBackButtonHidden(true)
Button(action: {
dismiss()
}, label: {
Text("Profile Subviewへ")
})
}
}
struct NextView: View {
@Environment(\.dismiss) private var dismiss
var body: some View {
VStack(spacing: 32) {
Text("Hello,NextView")
.font(.title)
Button {
dismiss()
} label: {
HStack {
Text("Nextへ")
Image(systemName: "chevron.up")
.resizable()
.frame(width: 30, height: 20)
}
}
}
}
}
struct NavigationBackItem: View {
@Environment(\.dismiss) private var dismissOn
var title : String = ""
var body: some View {
Button {
dismissOn()
} label: {
HStack {
Image(systemName: "lessthan")
.foregroundColor(.blue)
.padding(.leading, -4.0)
Text(title)
.foregroundColor(Color.blue)
.padding(.leading, -7.0)
}
}
}
}
struct ImageCarouselView: View {
private var numberOfImages: Int
private var content: Content
@State private var currentIndex: Int = 0
private let timer = Timer.publish(every: 3, on: .main, in: .common).autoconnect()
init(numberOfImages: Int, @ViewBuilder content: () -> Content) {
self.numberOfImages = numberOfImages
self.content = content()
}
var body: some View {
GeometryReader { geometry in
HStack(spacing: 0) {
self.content
}
.frame(width: geometry.size.width - 50, height: geometry.size.height - 50, alignment: .center)
.offset(x: CGFloat(self.currentIndex) * -geometry.size.width, y: 0)
.animation(.spring())
.onReceive(self.timer) { _ in
self.currentIndex = (self.currentIndex + 1) % 5
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
import SwiftUI
struct ContentView: View {
@State var selectedTag = 1
var body: some View {
TabView(selection: $selectedTag) {
HomeTabView().tag(1)
WeatherTabView().tag(2)
NewsTabView().tag(3)
NewsTabView2().tag(4)
NewsTabView3().tag(5)
}
.tabViewStyle(PageTabViewStyle())
.ignoresSafeArea()
}
}
struct HomeTabView: View {
var body: some View {
VStack {
Image(systemName: "music.note.house")
.scaleEffect(x: 3.0, y: 3.0)
.frame(width: 100, height: 100)
Text("HOME").font(.system(size: 40))
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color(red: 0.5, green: 0.9, blue: 0.9))
.ignoresSafeArea()
}
}
struct WeatherTabView: View {
var body: some View {
VStack {
Image(systemName: "cloud.sun")
.scaleEffect(x: 3.0, y: 3.0)
.frame(width: 100, height: 100)
Text("Weather").font(.system(size: 40))
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color(red: 1.0, green: 0.9, blue: 1.0))
.ignoresSafeArea()
}
}
struct NewsTabView: View {
var body: some View {
VStack {
Image(systemName: "newspaper")
.scaleEffect(x: 3.0, y: 3.0)
.frame(width: 100, height: 100)
Text("News").font(.system(size: 40))
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color(red: 0.9, green: 0.9, blue: 0.8))
.ignoresSafeArea()
}
}
struct NewsTabView2: View {
@State var isShowSubViw = false
var body: some View {
GeometryReader { geometory in
ZStack {
VStack {
Image(systemName: "ant.circle.fill")
.scaleEffect(x: 3.0, y: 3.0)
.frame(width: 100, height: 100)
Text("NewsTabView2")
.font(.system(size: 40, weight: .black, design: .default))
.padding(.top, -20)
}
if isShowSubViw {
SubView()
} else {
Button(action: {
withAnimation() {
self.isShowSubViw.toggle()
}
}) {
Text("SubViewへ遷移")
.padding(.top, 200)
}
}
}
.frame(width: geometory.size.width,
height: geometory.size.height)
.background(Color.green)
.animation(.easeInOut(duration: 0.42))
}
.transition(.move(edge: .leading))
}
}
struct SubView: View {
@State var isShowSubViw = false
var body: some View {
GeometryReader { geometory in
ZStack {
if isShowSubViw {
NewsTabView2()
} else {
Button(action: {
withAnimation() {
self.isShowSubViw.toggle()
}
}) {
Text("NewsTabView2へ戻る")
}
.padding(.top, 100)
VStack {
Text("SubView")
.font(.system(size: 40, weight: .black, design: .default))
}
}
.frame(width: geometory.size.width,
height: geometory.size.height)
.background(
LinearGradient(
gradient: Gradient(colors: [Color.yellow, Color.red]),
startPoint: .init(x: 0.3, y: 0.3), // start地点
endPoint: .init(x: 0.55, y: 0.55) // end地点 。
))
.animation(.easeInOut(duration: 0.42))
}
.transition(.move(edge: .leading))
}
}
struct NewsTabView3: View {
@State var isShow: Bool = false
var body: some View {
VStack {
Image(systemName: "ladybug.fill")
.scaleEffect(x: 3.0, y: 3.0)
.frame(width: 100, height: 100)
Text("First View").font(.system(size: 30))
.padding(.bottom, 30)
Button(action: {
withAnimation {
self.isShow.toggle()
}
}) {
Text("Second View pageへ").font(.system(size: 20))
}
.fullScreenCover(isPresented: $isShow){
SomeView(isPresented: $isShow) // SomeViewビューを表示する
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color.yellow)
.ignoresSafeArea()
}
}
struct SomeView: View {
// ContentViewビューの変数isShowとバインディングする
@Binding var isPresented: Bool
var body: some View {
NavigationView {
VStack {
Image(systemName: "ladybug").scaleEffect(2.0)
Text("Second View").font(.title2).padding()
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(
LinearGradient(
gradient: Gradient(colors: [Color.yellow, Color.green]),
startPoint: .init(x: 0.3, y: 0.3), // start地点
endPoint: .init(x: 0.55, y: 0.55) // end地点
))
.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
Button {
isPresented = false
} label: {
HStack {
Image(systemName:"lessthan")
.foregroundColor(.green)
Text("First Viewへ戻る")
.foregroundColor(Color.green)
}
}
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
import SwiftUI
extension Text {
func textModifier(color: Color) -> some View {
self
.font(.system(size: 14, design: .monospaced))
.fontWeight(.bold) // Viewに準拠していないModifier
.multilineTextAlignment(.center) // Viewに準拠していないModifier
.padding(7)
.overlay(RoundedRectangle(cornerRadius: 10).stroke(lineWidth: 1))
.foregroundColor(color)
}
}
struct ContentView: View {
let pi = 3.141519 // 円周率z
@State private var text: String = """
A text editor view allows you to display and edit multiline,scrollable text in your app’s user interface.
複数行のスクロール可能なテキストを表示・編集できる
"""
var body: some View {
VStack {
Text("myテキスト")
.textModifier(color: .red)
Text("円周率:\(pi, specifier: "%.2f")")
.font(.system(size: 40, weight: .black, design: .default))
.foregroundColor(.green)
.padding(.top, 10)
TextEditor(text: $text)
.frame(width: 320, height: 150)
.padding(.bottom, -20)
Text("Hello, world!").kerning(5)
.font(.custom("Times-Roman", size: 35))
.padding(.top, 20)
Text("エリア 200 x 200 の中に文章を中央寄りで表示します。よろしくお願いいたします。")
.frame(width: 280, height: 100, alignment: .leading)
.multilineTextAlignment(.leading)
.lineSpacing(10)
Text("SwiftUI")
.font(.system(size: 50, weight: .black, design: .default))
Divider().padding(.bottom, 10)
Text("SwiftUIで") +
Text("一部の文字").foregroundColor(.red).fontWeight(.bold) + Text("だけを装飾する。")
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
import SwiftUI
import SpriteKit
extension List {
func listBackground(_ color: Color) -> some View {
UITableView.appearance().backgroundColor = UIColor(color)
return self
}
}
struct ContentView: View {
private let myurl = URL(string: "https://tate.gif.jp/img/d01.jpg")
var body: some View {
NavigationView {
List {
Image("c03")
.resizable()
.aspectRatio(contentMode: .fill)
// .frame(maxWidth: .infinity, maxHeight: 40)
.frame(maxWidth: 40, maxHeight: 40)
.clipped()
HStack {
Circle()
.frame(width: 40, height: 40)
.foregroundColor(.green)
Text("mytitle")
}
NavigationLink(destination: GameA()) {
HStack() {
// Image("c03")
AsyncImage(url: myurl, scale: 7.2)
// .resizable()
.scaledToFill()
.frame(maxWidth: 40, maxHeight: 40)
.clipped()
.cornerRadius(4)
// Spacer(minLength: 1)
.padding(.trailing, 50)
Text("画像とテキストを表示")
}
} .listRowBackground(Color.orange)
NavigationLink(destination: SKContentView()) {
Text("Game").foregroundColor(.blue)
.fontWeight(.black)
}
.listRowBackground(Color.yellow)
NavigationLink(destination: Game2()) {
(Text("G") + Text("a")
.foregroundColor(Color.red)
.fontWeight(.black) + Text("me2"))
}
NavigationLink(destination: Game3()) {
Text("Game3")
.font(.custom("Times-Roman", size: 25))
}
.listRowBackground(Color.red)
NavigationLink(destination: Game4()) {
Text("Game4")
.font(.system(size: 20, weight: .black, design: .default))
}
// .listRowBackground(Color.clear)
.listRowBackground(Color(red: 0.1, green: 0.9, blue: 0.1, opacity: 0.6))
}.listBackground(.blue)
.listStyle(InsetListStyle())
// .navigationTitle("Game a player")
}
}
}
struct Game: View {
let screenWidth = UIScreen.main.bounds.width
let screenHeight = UIScreen.main.bounds.height
var scene: SKScene {
let scene = GameScene()
scene.size = CGSize(width: screenWidth, height: screenHeight)
scene.scaleMode = .fill
return scene
}
var body: some View {
SpriteView(scene: scene)
.frame(width: screenWidth, height: screenHeight)
.ignoresSafeArea()
}
}
struct Game2: View {
let screenWidth = UIScreen.main.bounds.width
let screenHeight = UIScreen.main.bounds.height
var scene: SKScene {
let scene = GameScene2()
scene.size = CGSize(width: screenWidth, height: screenHeight)
scene.scaleMode = .fill
return scene
}
var body: some View {
SpriteView(scene: scene)
.frame(width: screenWidth, height: screenHeight)
.ignoresSafeArea()
}
}
struct Game3: View {
let screenWidth = UIScreen.main.bounds.width
let screenHeight = UIScreen.main.bounds.height
var scene: SKScene {
let scene = GameScene3()
scene.size = CGSize(width: screenWidth, height: screenHeight)
scene.scaleMode = .fill
return scene
}
var body: some View {
SpriteView(scene: scene)
.frame(width: screenWidth, height: screenHeight)
.ignoresSafeArea()
}
}
struct Game4: View {
let screenWidth = UIScreen.main.bounds.width
let screenHeight = UIScreen.main.bounds.height
var scene: SKScene {
let scene = GameScene4()
scene.size = CGSize(width: screenWidth, height: screenHeight)
scene.scaleMode = .fill
return scene
}
var body: some View {
SpriteView(scene: scene)
.frame(width: screenWidth, height: screenHeight)
.ignoresSafeArea()
}
}
struct GameA: View {
let screenWidth = UIScreen.main.bounds.width
let screenHeight = UIScreen.main.bounds.height
var scene: SKScene {
let scene = GameSceneA()
scene.size = CGSize(width: screenWidth, height: screenHeight)
scene.scaleMode = .fill
return scene
}
var body: some View {
SpriteView(scene: scene)
.frame(width: screenWidth, height: screenHeight)
.ignoresSafeArea()
}
}
struct GameB: View {
let screenWidth = UIScreen.main.bounds.width
let screenHeight = UIScreen.main.bounds.height
var scene: SKScene {
let scene = GameSceneB()
scene.size = CGSize(width: screenWidth, height: screenHeight)
scene.scaleMode = .fill
return scene
}
var body: some View {
SpriteView(scene: scene)
.frame(width: screenWidth, height: screenHeight)
.ignoresSafeArea()
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
///////////////////
GameScene
import SwiftUI
import SpriteKit
class GameScene: SKScene {
override func didMove(to view: SKView) {
backgroundColor = .yellow
let TitleLabel = SKLabelNode(fontNamed: "Verdana-bold")
TitleLabel.text = "GameScene"
TitleLabel.fontSize = 30
TitleLabel.horizontalAlignmentMode = .center
TitleLabel.fontColor = SKColor.black
TitleLabel.position = CGPoint(x:175, y:370)
self.addChild(TitleLabel)
}
override func touchesMoved(_ touches: Set, with event: UIEvent?) {
}
}
struct SKContentView: View {
let screenWidth = UIScreen.main.bounds.width
let screenHeight = UIScreen.main.bounds.height
@Environment(\.dismiss) var dismiss
var name: String = "Hello SwiftUI!"
var scene: SKScene {
let scene = GameScene()
scene.size = CGSize(width: screenWidth, height: screenHeight)
scene.scaleMode = .fill
return scene
}
var body: some View {
ZStack {
Color.blue
.ignoresSafeArea()
VStack {
SpriteView(scene: scene)
.frame(width: screenWidth, height: screenHeight)
.ignoresSafeArea()
}
.navigationBarBackButtonHidden(true)
.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
Button(
action: {
dismiss()
}, label: {
Image(systemName: "arrow.backward")
Text("戻る")
}
).tint(.blue)
}
}
}
}
}
struct SKContentView_Previews: PreviewProvider {
static var previews: some View {
SKContentView()
}
///////////////////////
GameScene3
class GameScene3: SKScene {
let NextLabel = SKLabelNode(fontNamed: "Verdana-bold")
override func didMove(to view: SKView) {
backgroundColor = .red
let TitleLabel = SKLabelNode(fontNamed: "Verdana-bold")
TitleLabel.text = "GameScene3"
TitleLabel.fontSize = 30
TitleLabel.horizontalAlignmentMode = .center
TitleLabel.fontColor = SKColor.black
TitleLabel.position = CGPoint(x:175, y:370)
self.addChild(TitleLabel)
NextLabel.text = "SceneA-bへGO"
NextLabel.fontSize = 30
NextLabel.horizontalAlignmentMode = .center
NextLabel.fontColor = SKColor.black
NextLabel.position = CGPoint(x:175, y:570)
self.addChild(NextLabel)
}
override func touchesBegan(_ touches: Set, with event: UIEvent?) {
let breakOutGameScene = GameScene4(size: self.size)
self.view?.presentScene(breakOutGameScene)
}
}
//////////////////////
GameScene4
import SwiftUI
import SpriteKit
class GameScene4: SKScene {
override func didMove(to view: SKView) {
backgroundColor = .green
let TitleLabel = SKLabelNode(fontNamed: "Verdana-bold")
TitleLabel.text = "GameSceneAへ"
TitleLabel.fontSize = 30
TitleLabel.horizontalAlignmentMode = .center
TitleLabel.fontColor = SKColor.black
TitleLabel.position = CGPoint(x:175, y:370)
TitleLabel.name = "tail"
self.addChild(TitleLabel)
}
override func touchesBegan(_ touches: Set, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
let touchedNode = atPoint(location)
if touchedNode.name == "tail" {
// let reveal = SKTransition.doorway(withDuration: 1)
let scene = GameSceneA(size: self.size)
let skView = self.view!
scene.scaleMode = SKSceneScaleMode.aspectFit
// skView.presentScene(scene, transition: reveal)
skView.presentScene(scene)
}
}
}
}
//////////////////////////////////////
GameSceneA
import SpriteKit
import GameplayKit
class GameSceneA: SKScene {
//各種ラベルの準備
let countLabel = SKLabelNode(fontNamed: "Verdana-bold")
let TitleLabel = SKLabelNode(fontNamed: "Verdana-bold")
let NextLabel = SKLabelNode(fontNamed: "Verdana-bold")
override func didMove(to view: SKView) {
self.backgroundColor = UIColor.cyan
TitleLabel.text = "タイトルA"
TitleLabel.fontSize = 30
TitleLabel.horizontalAlignmentMode = .center
TitleLabel.fontColor = SKColor.black
TitleLabel.position = CGPoint(x:175, y:670)
self.addChild(TitleLabel)
NextLabel.text = "GameSceneBへGO"
NextLabel.fontSize = 30
NextLabel.horizontalAlignmentMode = .center
NextLabel.fontColor = SKColor.black
NextLabel.position = CGPoint(x:175, y:470)
NextLabel.name = "label"
self.addChild(NextLabel)
// countLabel.text = "タップは\(appDelegate.count)回"
countLabel.fontSize = 40
countLabel.horizontalAlignmentMode = .center
countLabel.fontColor = SKColor.black
countLabel.position = CGPoint(x:175, y:70)
self.addChild(countLabel)
}
func touchDown(atPoint pos : CGPoint) {
}
func touchMoved(toPoint pos : CGPoint) {
}
func touchUp(atPoint pos : CGPoint) {
}
override func touchesBegan(_ touches: Set, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
let touchedNode = atPoint(location)
if touchedNode.name == "label" {
// let reveal = SKTransition.doorway(withDuration: 1)
let scene = GameSceneB(size: self.size)
let skView = self.view!
scene.scaleMode = SKSceneScaleMode.aspectFit
// skView.presentScene(scene, transition: reveal)
skView.presentScene(scene)
}
}
}
override func touchesMoved(_ touches: Set, with event: UIEvent?) {
}
override func touchesEnded(_ touches: Set, with event: UIEvent?) {
}
override func touchesCancelled(_ touches: Set, with event: UIEvent?) {
}
override func update(_ currentTime: TimeInterval) {
}
}
//////////////////////
WebViewTest.swift
import SwiftUI
import WebKit //WebKitをインポート
struct WebViewTest: UIViewRepresentable {
// var url: String = "https://google.com" //表示するWEBページのURLを指定
func makeUIView(context: Context) -> WKWebView{
return WKWebView()
}
func updateUIView(_ uiView: UIViewType, context: Context) {
guard let path: String = Bundle.main.path(forResource: "index", ofType: "html") else { return }
let localHTMLUrl = URL(fileURLWithPath: path, isDirectory: false)
uiView.loadFileURL(localHTMLUrl, allowingReadAccessTo: localHTMLUrl)
}
}
///////////////////////////
ContentView.swift
import SwiftUI
import WebKit
struct ContentView: View {
var body: some View {
NavigationView {
NavigationLink(destination: WebViewTest()) {
Text("Show web page")
}
}
// WebViewTest()
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
//////////////////////////////
index.html