Skip to content

Instantly share code, notes, and snippets.

@maiyama18
Created August 17, 2022 15:22
Show Gist options
  • Save maiyama18/51accab113cb387993cb985e6da8cbc2 to your computer and use it in GitHub Desktop.
Save maiyama18/51accab113cb387993cb985e6da8cbc2 to your computer and use it in GitHub Desktop.
//
// Topic006.swift
// SwiftUIWeeklyLayoutChallenge
//
// Created by treastrain on 2022/08/17.
//
import SwiftUI
fileprivate func +(left: CGSize, right: CGSize) -> CGSize {
.init(width: left.width + right.width, height: left.height + right.height)
}
/// <doc:Topic006>
public struct Topic006View: View {
public init() {}
public var body: some View {
#if os(iOS)
if #available(iOS 15.0, *) {
Topic006ContentView()
} else {
Text("Support for this platform is not considered.")
}
#else
Text("Support for this platform is not considered.")
#endif
}
}
#if os(iOS)
@available(iOS 15.0, *)
fileprivate struct Topic006ContentView: View {
@State private var isCreateNewPresented: Bool = false
@State private var isAttachImagePresented: Bool = false
@State private var isAttachSoundPresented: Bool = false
@State private var previousOffset: CGSize = .init(width: -16, height: -16)
@State private var draggingTranslation: CGSize = .zero
var body: some View {
ZStack(alignment: .bottomTrailing) {
Text("後ろに位置する View")
.frame(maxWidth: .infinity, maxHeight: .infinity)
Menu(content: {
Button(action: {
isCreateNewPresented = true
}) {
HStack {
Label("新規作成", systemImage: "square.and.pencil")
}
}
Button(action: {
isAttachImagePresented = true
}) {
HStack {
Label("画像を追加", systemImage: "photo.on.rectangle")
}
}
Button(action: {
isAttachSoundPresented = true
}) {
HStack {
Label("音声を追加", systemImage: "mic.badge.plus")
}
}
}, label: {
Circle()
.fill((isCreateNewPresented || isAttachSoundPresented || isAttachImagePresented) ? .gray : .blue)
.frame(width: 52, height: 52)
.overlay(
Image(systemName: "plus")
.font(.system(size: 24))
.foregroundColor(.white)
)
}, primaryAction: {
isCreateNewPresented = true
})
.offset(previousOffset + draggingTranslation)
.gesture(
DragGesture()
.onChanged { value in
draggingTranslation = value.translation
}
.onEnded { value in
draggingTranslation = .zero
previousOffset = previousOffset + value.translation
}
)
}
.alert("新規作成", isPresented: $isCreateNewPresented) {
Button(action: { isCreateNewPresented = false }) { Text("閉じる") }
}
.alert("画像を追加", isPresented: $isAttachImagePresented) {
Button(action: { isAttachImagePresented = false }) { Text("閉じる") }
}
.alert("音声を追加", isPresented: $isAttachSoundPresented) {
Button(action: { isAttachSoundPresented = false }) { Text("閉じる") }
}
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment