Skip to content

Commit

Permalink
Added flippable view
Browse files Browse the repository at this point in the history
  • Loading branch information
mszpro committed Mar 21, 2024
1 parent 87603b2 commit 63fb6f1
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
3 changes: 0 additions & 3 deletions Sources/SwiftUILibrary.docc/SwiftUILibrary.md

This file was deleted.

65 changes: 65 additions & 0 deletions Sources/SwiftUILibrary/FlippableView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//
// FlippableView.swift
// AIVocabularyBook
//
// Created by Msz on 2023/05/10.
//

import SwiftUI

@available(iOS 13.0, *)
@available(macOS 11, *)
@available(watchOS 6.0, *)
struct FlippableView<Front: View, Back: View>: View {

var cardFrontView: Front
var cardBackView: Back

@State private var isFlipped = false
private let animationDuration = 0.3

init(@ViewBuilder front: () -> Front, @ViewBuilder back: () -> Back) {
self.cardFrontView = front()
self.cardBackView = back()
}

var body: some View {
VStack {
Spacer()
ZStack {
if isFlipped {
cardBackView
.rotation3DEffect(.degrees(180), axis: (x: 0.0, y: 1.0, z: 0.0))
} else {
cardFrontView
}
}
.rotation3DEffect(.degrees(isFlipped ? 180 : 0), axis: (x: 0.0, y: 1.0, z: 0.0))
.animation(.easeInOut(duration: animationDuration), value: isFlipped)
.onTapGesture {
withAnimation {
self.isFlipped.toggle()
}
}
Spacer()
}
}
}


struct FlippableView_Previews: PreviewProvider {
static var previews: some View {
FlippableView {
Text("Front of card")
.padding()
.foregroundColor(.white)
.background(RoundedRectangle(cornerRadius: 10).foregroundColor(.blue))
} back: {
Text("Back of card")
.padding()
.foregroundColor(.white)
.background(RoundedRectangle(cornerRadius: 10).foregroundColor(.red))
}

}
}

0 comments on commit 63fb6f1

Please sign in to comment.