Skip to content

Commit

Permalink
#43 Card Slider Demo 제작 완료
Browse files Browse the repository at this point in the history
  • Loading branch information
doyeonk429 committed Aug 16, 2024
1 parent 3739e50 commit 6f7c7a9
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 25 deletions.
12 changes: 12 additions & 0 deletions Drink-EG/Drink-EG.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
1654011F2C6E56A9008A9DBF /* UIViewExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1654011E2C6E56A9008A9DBF /* UIViewExtension.swift */; };
165401212C6E56BD008A9DBF /* RatingView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 165401202C6E56BD008A9DBF /* RatingView.swift */; };
165401232C6E57BC008A9DBF /* CardSliderCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 165401222C6E57BC008A9DBF /* CardSliderCell.swift */; };
1654014C2C6F41B0008A9DBF /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1654014B2C6F41B0008A9DBF /* ContentView.swift */; };
16752F9D2C5A7FB90001907D /* HexCode.swift in Sources */ = {isa = PBXBuildFile; fileRef = 16752F9C2C5A7FB90001907D /* HexCode.swift */; };
16B97CD42C4BEB0900D8B453 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 16B97CD32C4BEB0900D8B453 /* AppDelegate.swift */; };
16B97CD62C4BEB0900D8B453 /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 16B97CD52C4BEB0900D8B453 /* SceneDelegate.swift */; };
Expand Down Expand Up @@ -147,6 +148,7 @@
1654011E2C6E56A9008A9DBF /* UIViewExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = UIViewExtension.swift; path = ../../../../../../../../Downloads/UIViewExtension.swift; sourceTree = "<group>"; };
165401202C6E56BD008A9DBF /* RatingView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = RatingView.swift; path = ../../../../../../../../Downloads/RatingView.swift; sourceTree = "<group>"; };
165401222C6E57BC008A9DBF /* CardSliderCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = CardSliderCell.swift; path = ../../../../../../../../Downloads/CardSliderCell.swift; sourceTree = "<group>"; };
1654014B2C6F41B0008A9DBF /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
16752F9C2C5A7FB90001907D /* HexCode.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HexCode.swift; sourceTree = "<group>"; };
16B97CD02C4BEB0900D8B453 /* Drink-EG.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Drink-EG.app"; sourceTree = BUILT_PRODUCTS_DIR; };
16B97CD32C4BEB0900D8B453 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -290,6 +292,14 @@
path = CustomCardSlider;
sourceTree = "<group>";
};
165401482C6F415F008A9DBF /* CardSliderSwiftUI */ = {
isa = PBXGroup;
children = (
1654014B2C6F41B0008A9DBF /* ContentView.swift */,
);
path = CardSliderSwiftUI;
sourceTree = "<group>";
};
16B97CC72C4BEB0900D8B453 = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -465,6 +475,7 @@
16B97CF82C4BFB2400D8B453 /* WineClass */ = {
isa = PBXGroup;
children = (
165401482C6F415F008A9DBF /* CardSliderSwiftUI */,
16B97D112C4C0F2400D8B453 /* WineClassMainViewController.swift */,
16B97D132C4C0F4100D8B453 /* ClassVideoViewController.swift */,
16B97D152C4C0F6400D8B453 /* SavingVideoViewController.swift */,
Expand Down Expand Up @@ -722,6 +733,7 @@
1F598EFB2C5814F9000CE79F /* EnterLoginViewController.swift in Sources */,
16B97D022C4BFC9800D8B453 /* SearchHomeViewController.swift in Sources */,
5E2219732C6B661A005849FB /* CheckNoteViewController.swift in Sources */,
1654014C2C6F41B0008A9DBF /* ContentView.swift in Sources */,
5E2219442C69D5D0005849FB /* CreateNewCommViewController.swift in Sources */,
5E2219472C69D5D0005849FB /* CustomCollectionViewCell.swift in Sources */,
5E22195B2C69D5E7005849FB /* WriteNoteViewController.swift in Sources */,
Expand Down
1 change: 0 additions & 1 deletion Drink-EG/Drink-EG/Resources/SceneDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
// 화면을 구성하는 UIWindow 인스턴스 생성
let window = UIWindow(windowScene: windowScene)
// 실제 첫 화면이 되는 MainViewController 인스턴스 생성
// let vc = ThirdVarietyTasteTestViewController()
let vc = MainTabBarViewController()
// NavigationController을 사용할 경우, MainViewController를 rootViewController로 갖는 NavigationController을 생성해야한다.
let navigationController = UINavigationController(rootViewController: vc)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
//
// ContentView.swift
// Drink-EG
//
// Created by 김도연 on 8/16/24.
//
import SwiftUI

struct ContentView: View {
struct Card : Identifiable {
var id : Int
var name: String
var poster: String
var offset: CGFloat
}

@State var cards = [
Card(id: 0, name: "card1", poster: "Dos Copas", offset: 0),
Card(id: 1, name: "card2", poster: "Dos Copas", offset: 0),
Card(id: 2, name: "card3", poster: "Dos Copas", offset: 0)
]

@State var move = 0

var body: some View {
GeometryReader { geometry in
ZStack {
ForEach(cards.reversed()) { card in
VStack {
Image(card.poster)
.resizable()
.aspectRatio(contentMode: .fill)
.frame(
width: geometry.size.width * 0.7, // 화면 너비에 비례하여 설정
height: geometry.size.height * 0.5 - CGFloat(card.id - move) * 40
)
.cornerRadius(15)
.shadow(radius: 10)
.offset(x: card.id - move <= 2 ? CGFloat(card.id - move) * 20 : 60)

Text("\(card.name)")
.font(.footnote)
}
.offset(x: card.offset)
.frame(width: geometry.size.width, height: geometry.size.height)
.position(x: geometry.size.width / 2, y: geometry.size.height / 2)
.gesture(DragGesture().onChanged({ value in
withAnimation {
if value.translation.width < 0 {
if -value.translation.width < 0 && card.id != cards.last?.id {
cards[card.id].offset = value.translation.width
} else {
if card.id > 0 {
cards[card.id].offset = -((geometry.size.width - 50) + 60) + value.translation.width
}
}
}
}
}).onEnded({ value in
withAnimation {
if value.translation.width < 0 {
if -value.translation.width > 180 && card.id != cards.last?.id {
cards[card.id].offset = -((geometry.size.width - 50) + 60)
move += 1
} else {
cards[card.id].offset = 0
}
} else {
if card.id > 0 {
cards[card.id - 1].offset = -((geometry.size.width - 50) + 60)

if value.translation.width > 180 {
cards[card.id - 1].offset = 0
move -= 1
} else {
cards[card.id].offset = -((geometry.size.width - 50) + 60)
}
}
}
}
}))

}
}
}
.edgesIgnoringSafeArea(.all) // GeometryReader가 전체 화면을 사용하도록 설정
}
}

#Preview {
ContentView()
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,29 @@

import UIKit
import SnapKit
import SwiftUI

struct Item: CardSliderItem {
var image: UIImage
var rating: Int?
var title: String
var subtitle: String?
var description: String?
}

class WineClassMainViewController : CardSliderViewController, CardSliderDataSource {
var datas = [Item]()

class WineClassMainViewController : UIViewController {
override func viewDidLoad() {
super.viewDidLoad()

datas.append(Item(image: UIImage(named: "ClassSampleImage")!, title: "First Wine Class"))
datas.append(Item(image: UIImage(named: "ClassSampleImage")!, title: "Second Wine Class"))
datas.append(Item(image: UIImage(named: "ClassSampleImage")!, title: "Third Wine Class"))

view.backgroundColor = .white

self.dataSource = self
setUI()

}

func item(for index: Int) -> any CardSliderItem {
datas[index]
}

func numberOfItems() -> Int {
datas.count
private func setUI() {
let cardSliderView = ContentView()
let hostingController = UIHostingController(rootView: cardSliderView)

self.addChild(hostingController)
self.view.addSubview(hostingController.view)
hostingController.didMove(toParent: self)

hostingController.view.snp.makeConstraints { make in
make.center.equalToSuperview() // 화면 정중앙에 배치
make.width.equalToSuperview().multipliedBy(0.8) // 부모 뷰의 80% 너비
make.height.equalToSuperview().multipliedBy(0.8) // 부모 뷰의 50% 높이
}
}

}

0 comments on commit 6f7c7a9

Please sign in to comment.