Skip to content

Instantly share code, notes, and snippets.

@azamsharp
Created August 7, 2020 22:45
Show Gist options
  • Save azamsharp/60afd306d09d87a3ea05d82a513cb1ec to your computer and use it in GitHub Desktop.
Save azamsharp/60afd306d09d87a3ea05d82a513cb1ec to your computer and use it in GitHub Desktop.
//
// ContentView.swift
// Shared
//
// Created by Mohammad Azam on 7/21/20.
//
import SwiftUI
struct ContentView: View {
@State private var degrees: Double = 0
@State private var flipped: Bool = false
@State private var name: String = ""
@State private var expires: String = ""
@State private var cvv: String = ""
var body: some View {
VStack {
CreditCard {
VStack {
Group {
if flipped {
CreditCardBack(cvv: cvv)
} else {
CreditCardFront(name: name, expires: expires)
}
}
}.rotation3DEffect(
.degrees(degrees),
axis: (x: 0.0, y: 1.0, z: 0.0)
)
}
.onTapGesture {
withAnimation {
degrees += 180
flipped.toggle()
}
}
TextField("Name", text: $name)
.textFieldStyle(RoundedBorderTextFieldStyle())
.padding([.top,.leading,.trailing])
TextField("Expiration", text: $expires)
.textFieldStyle(RoundedBorderTextFieldStyle())
.padding([.leading,.trailing])
TextField("CVV", text: $cvv) { (editingChanged) in
withAnimation {
degrees += 180
flipped.toggle()
}
} onCommit: {}
.textFieldStyle(RoundedBorderTextFieldStyle())
.padding([.leading,.trailing])
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
//
// CreditCard.swift
// CreditCardSwiftUI
//
// Created by Mohammad Azam on 7/21/20.
//
import SwiftUI
struct CreditCard<Content>: View where Content: View {
var content: () -> Content
var body: some View {
content()
}
}
struct CreditCardFront: View {
let name: String
let expires: String
var body: some View {
VStack(alignment: .leading) {
HStack(alignment: .top) {
Image(systemName: "checkmark.circle.fill")
.foregroundColor(Color.white)
Spacer()
Text("VISA")
.foregroundColor(Color.white)
.font(.system(size: 24))
.fontWeight(.bold)
}
Spacer()
Text("**** **** **** 2864")
.foregroundColor(Color.white)
.font(.system(size: 32))
Spacer()
HStack {
VStack(alignment: .leading) {
Text("CARD HOLDER")
.font(.caption)
.fontWeight(.bold)
.foregroundColor(Color.gray)
Text(name)
.font(.caption)
.fontWeight(.bold)
.foregroundColor(Color.white)
}
Spacer()
VStack {
Text("EXPIRES")
.font(.caption)
.fontWeight(.bold)
.foregroundColor(Color.gray)
Text(expires)
.font(.caption)
.fontWeight(.bold)
.foregroundColor(Color.white)
}
}
}.frame(width: 300, height: 200)
.padding()
.background(LinearGradient(gradient: Gradient(colors: [Color(#colorLiteral(red: 0.5481430292, green: 0, blue: 0.4720868468, alpha: 1)), Color.blue]), startPoint: .topLeading, endPoint: .bottomTrailing))
.cornerRadius(10)
}
}
struct CreditCardBack: View {
let cvv:String
var body: some View {
VStack {
Rectangle()
.frame(maxWidth: .infinity, maxHeight: 20)
.padding([.top])
Spacer()
HStack {
Text(cvv).foregroundColor(Color.black)
.rotation3DEffect(
.degrees(180),
axis: (x: 0.0, y: 1.0, z: 0.0))
.padding(5)
.frame(width: 100, height: 20)
.background(Color.white)
Spacer()
}.padding()
}.frame(width: 300, height: 200)
.background(LinearGradient(gradient: Gradient(colors: [Color.yellow, Color.blue]), startPoint: /*@START_MENU_TOKEN@*/.leading/*@END_MENU_TOKEN@*/, endPoint: /*@START_MENU_TOKEN@*/.trailing/*@END_MENU_TOKEN@*/))
.cornerRadius(10)
}
}
struct CreditCard_Previews: PreviewProvider {
static var previews: some View {
CreditCard<CreditCardFront>(content: { CreditCardFront(name: "Mohammad Azam", expires: "02/06") })
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment