Skip to content

Instantly share code, notes, and snippets.

@Codelaby
Last active June 27, 2023 12:42
Show Gist options
  • Save Codelaby/99f01740bb4c54d5fb86c355e249068e to your computer and use it in GitHub Desktop.
Save Codelaby/99f01740bb4c54d5fb86c355e249068e to your computer and use it in GitHub Desktop.
Swift Samples
//
// PostionSampleView.swift
// Avatars
//
// Created by Codelaby on 25/6/23.
//
import SwiftUI
struct HStackSample: View {
var body: some View {
//.top .center .bottom
HStack(alignment: .bottom) {
Text("first item")
.border(.red)
Text("second item\n and second line of second item and wrapper text")
.border(.green)
Text("third item")
.border(.blue)
}
}
}
struct HeroRow : View {
let avatar : String
let title : String
let supportText: String
let actionTime: String
@State private var isLiked = false
var body: some View {
HStack() {
Image(avatar)
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: 48, height: 48)
.clipShape(Circle())
.padding(.leading, 16)
VStack(alignment: .leading) {
Text(title)
.font(.headline)
Text(supportText)
.font(.subheadline)
+ Text(" • \(actionTime)")
.font(.system(.subheadline))
.foregroundColor(.secondary)
}
.padding(.leading, 8)
}
//.padding()
.frame(maxWidth: .infinity, maxHeight: 72, alignment: .leading)
.background(.thinMaterial.opacity(0.9))
.clipShape(RoundedRectangle(cornerRadius: 24))
.environment(\.colorScheme, .dark)
}
}
struct PositionSampleView: View {
var body: some View {
ZStack(alignment: .center) {
AsyncImage(url: URL(string: "https://media.timeout.com/images/105737732/image.jpg")) { phase in
switch phase {
case .empty:
ProgressView()
case .success(let image):
image
.resizable()
.scaledToFill()
.aspectRatio(contentMode: .fill)
.frame(maxWidth: .infinity, maxHeight: 260)
.clipped()
case .failure(let error):
Text("Error: \(error.localizedDescription)")
@unknown default:
ProgressView() // Handle unknown cases with a fallback view or behavior
}
}
//.padding()
VStack() {
HeroRow(avatar: "AvatarGirl", title: "Victoria", supportText: "Posted a photo", actionTime: "21h")
.padding()
Spacer()
//HStackSample()
}
}
.clipShape(RoundedRectangle(cornerRadius: 24))
.padding()
.frame(maxWidth: .infinity, maxHeight: 260)
//.border(.purple)
}
}
struct PositionSampleView_Previews: PreviewProvider {
static var previews: some View {
PositionSampleView()
Group() {
HeroRow(avatar: "AvatarGirl", title: "Victoria", supportText: "Posted a photo", actionTime: "21h")
.environment(\.layoutDirection, .leftToRight)
HeroRow(avatar: "AvatarGirl", title: "Victoria", supportText: "Posted a photo", actionTime: "21h")
.environment(\.layoutDirection, .rightToLeft)
}
}
}
//
// oldest.swift
// Avatars
//
// Created by Codelaby on 25/6/23.
//
/*
https://karinprater.medium.com/swiftui-tutorial-layout-tools-211b9663befe
https://smashswift.com/how-to-position-view-related-to-parent-in-swiftui/
https://stackoverflow.com/questions/64291131/align-items-in-swiftui-stack-evenly
https://www.hackingwithswift.com/quick-start/swiftui/how-to-inset-the-safe-area-with-custom-content
https://www.swiftanytime.com/blog/geometry-reader-in-swiftui
https://github.com/jboullianne/CreditCardView/blob/master/Example/CreditCardView/ViewController.swift
*/
import Foundation
struct VStackSample: View {
var body: some View {
//alignment: .leading .center .trailing
VStack(alignment: .trailing) {
Text("first item")
.border(.red)
Text("second item and more text")
.border(.green)
Text("third item")
.border(.blue)
}
}
}
struct HStackSample: View {
var body: some View {
//.top .center .bottom
HStack(alignment: .top) {
Text("first item")
.border(.red)
Text("second item\n and second line of second item and wrapper text")
.border(.green)
Text("third item")
.border(.blue)
}
}
}
struct ZStackSample: View {
var body: some View {
//.top .center .bottom
ZStack(alignment: .topLeading) {
Text("1")
.frame(width: 200, height: 200)
.border(.red)
Text("2")
.frame(width: 100, height: 100)
.border(.green)
Text("3")
.frame(width: 50, height: 50)
.border(.blue)
}
}
}
struct VStackSample2: View {
var body: some View {
VStack() {
//Spacer()
Text("1")
.frame(width: 50, height: 50)
.border(.red)
//Spacer()
Text("2")
.frame(width: 50, height: 50)
.border(.green)
//Spacer()
Text("3")
.frame(width: 50, height: 50)
.border(.blue)
//Spacer()
}
}
}
struct HStackSample2: View {
var body: some View {
HStack() {
//Spacer()
Text("1")
.frame(width: 50, height: 50)
.border(.red)
//Spacer()
Text("2")
.frame(width: 50, height: 50)
.border(.green)
//Spacer()
Text("3")
.frame(width: 50, height: 50)
.border(.blue)
//Spacer()
}
}
}
struct VisualEffect: UIViewRepresentable {
@State var style: UIBlurEffect.Style
var blurIntensity: CGFloat
func makeUIView(context: Context) -> UIVisualEffectView {
let effect = UIBlurEffect(style: style)
let effectView = UIVisualEffectView(effect: effect)
effectView.alpha = blurIntensity
return effectView
}
func updateUIView(_ uiView: UIVisualEffectView, context: Context) {
uiView.alpha = blurIntensity
}
}
struct ZStackSample2: View {
var body: some View {
ZStack(alignment: .topLeading) {
VStack(alignment: .leading) {
HStack {
Text("aaaa")
.frame(maxWidth: .infinity, maxHeight: 50)
.border(Color.black)
//Spacer()
}
Spacer()
HStack(alignment: .bottom) {
Text("bbbb")
.frame(width: 100, height: 100)
.border(Color.black)
Spacer()
Text("cccc")
.frame(width: 150, height: 150)
.border(Color.black)
}
}
.padding()
}
//.padding()
.frame(maxWidth: .infinity, maxHeight: 320)
}
}
struct PhotoCard: View {
var body: some View {
//.top .center .bottom
ZStack(alignment: .center) {
AsyncImage(url: URL(string: "https://media.timeout.com/images/105737732/image.jpg")) { phase in
switch phase {
case .empty:
ProgressView()
case .success(let image):
image
.resizable()
.scaledToFill()
.frame(width: (UIScreen.main.bounds.width) - 32, height: (UIScreen.main.bounds.width * 9/16))
.clipped()
case .failure(let error):
Text("Error: \(error.localizedDescription)")
@unknown default:
ProgressView() // Handle unknown cases with a fallback view or behavior
}
}
.clipShape(RoundedRectangle(cornerRadius: 20))
Text("Hello, World!")
.frame(width: 150, height: 50, alignment: .center)
.background(.thinMaterial)
.background(
VisualEffect(style: .regular, blurIntensity: 0.8)
.opacity(1)
)
VStack(alignment: .trailing) {
//Spacer()
HStack() {
//Spacer()
Text("1")
.frame(width: 200, height: 200)
.border(.red)
//Spacer()
}
//Spacer()
}
Text("2")
.frame(width: 100, height: 100)
.border(.green)
Text("3")
.frame(width: 50, height: 50)
.border(.blue)
}
.frame( maxWidth: .infinity)
}
}
struct OldestampleView: View {
var body: some View {
//PhotoCard()
//VStackSample()
//HStackSample()
//ZStackSample()
//VStackSample2()
//HStackSample2()
ZStackSample2()
//.frame( maxWidth: .infinity, maxHeight: .infinity)
//.padding()
.border(.purple)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment