From a9bc84505ad66c8ecc6af5ed768f3e404e8e48ca Mon Sep 17 00:00:00 2001 From: Dorian <61510923+dodoleouf@users.noreply.github.com> Date: Sun, 23 Jun 2024 23:47:48 +0200 Subject: [PATCH 1/3] =?UTF-8?q?Create:=20GradientButtonStyle=20file=20for?= =?UTF-8?q?=20custom=20button=20style=20=F0=9F=8E=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HikeApp.xcodeproj/project.pbxproj | 4 ++++ HikeApp/Utility/GradientButtonStyle.swift | 8 ++++++++ 2 files changed, 12 insertions(+) create mode 100644 HikeApp/Utility/GradientButtonStyle.swift diff --git a/HikeApp.xcodeproj/project.pbxproj b/HikeApp.xcodeproj/project.pbxproj index 5e883ba..5bd91de 100644 --- a/HikeApp.xcodeproj/project.pbxproj +++ b/HikeApp.xcodeproj/project.pbxproj @@ -11,6 +11,7 @@ F4610AD42C28B67A0095D75F /* CustomBackgroundView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4610AD32C28B67A0095D75F /* CustomBackgroundView.swift */; }; F4610AD72C28B78C0095D75F /* ColorExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4610AD62C28B78C0095D75F /* ColorExtension.swift */; }; F4610AD92C28BE7A0095D75F /* CustomButtonView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4610AD82C28BE7A0095D75F /* CustomButtonView.swift */; }; + F4610ADB2C28CA730095D75F /* GradientButtonStyle.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4610ADA2C28CA730095D75F /* GradientButtonStyle.swift */; }; F4A0A8822C1F800B003AFEA1 /* HikeApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4A0A8812C1F800B003AFEA1 /* HikeApp.swift */; }; F4A0A8842C1F800B003AFEA1 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F4A0A8832C1F800B003AFEA1 /* ContentView.swift */; }; F4A0A8862C1F800D003AFEA1 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = F4A0A8852C1F800D003AFEA1 /* Assets.xcassets */; }; @@ -22,6 +23,7 @@ F4610AD32C28B67A0095D75F /* CustomBackgroundView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomBackgroundView.swift; sourceTree = ""; }; F4610AD62C28B78C0095D75F /* ColorExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ColorExtension.swift; sourceTree = ""; }; F4610AD82C28BE7A0095D75F /* CustomButtonView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomButtonView.swift; sourceTree = ""; }; + F4610ADA2C28CA730095D75F /* GradientButtonStyle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GradientButtonStyle.swift; sourceTree = ""; }; F4A0A87E2C1F800B003AFEA1 /* HikeApp.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = HikeApp.app; sourceTree = BUILT_PRODUCTS_DIR; }; F4A0A8812C1F800B003AFEA1 /* HikeApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HikeApp.swift; sourceTree = ""; }; F4A0A8832C1F800B003AFEA1 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = ""; }; @@ -54,6 +56,7 @@ isa = PBXGroup; children = ( F4610AD62C28B78C0095D75F /* ColorExtension.swift */, + F4610ADA2C28CA730095D75F /* GradientButtonStyle.swift */, ); path = Utility; sourceTree = ""; @@ -169,6 +172,7 @@ F43259AF2C22279E00F258E5 /* CardView.swift in Sources */, F4A0A8822C1F800B003AFEA1 /* HikeApp.swift in Sources */, F4610AD42C28B67A0095D75F /* CustomBackgroundView.swift in Sources */, + F4610ADB2C28CA730095D75F /* GradientButtonStyle.swift in Sources */, F4610AD72C28B78C0095D75F /* ColorExtension.swift in Sources */, F4610AD92C28BE7A0095D75F /* CustomButtonView.swift in Sources */, ); diff --git a/HikeApp/Utility/GradientButtonStyle.swift b/HikeApp/Utility/GradientButtonStyle.swift new file mode 100644 index 0000000..110e00f --- /dev/null +++ b/HikeApp/Utility/GradientButtonStyle.swift @@ -0,0 +1,8 @@ +// +// GradientButtonStyle.swift +// HikeApp +// +// Created by Dorian Emenir on 23/06/2024. +// + +import Foundation From ce0ccaaeb9c61ebc20fb8d7f395c0b1c55dd55c6 Mon Sep 17 00:00:00 2001 From: Dorian <61510923+dodoleouf@users.noreply.github.com> Date: Sun, 23 Jun 2024 23:48:52 +0200 Subject: [PATCH 2/3] =?UTF-8?q?Update:=20add=20a=20"Explore=20More"=20butt?= =?UTF-8?q?on=20&=20a=20random=20image=20generator=20to=20change=20the=20d?= =?UTF-8?q?isplayed=20image=20when=20clicked=20=F0=9F=91=B7=F0=9F=8F=BB?= =?UTF-8?q?=E2=80=8D=E2=99=82=EF=B8=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HikeApp/Component/CardView.swift | 57 +++++++++++++++++++++++++++++--- 1 file changed, 53 insertions(+), 4 deletions(-) diff --git a/HikeApp/Component/CardView.swift b/HikeApp/Component/CardView.swift index bea8d48..46b4fbb 100644 --- a/HikeApp/Component/CardView.swift +++ b/HikeApp/Component/CardView.swift @@ -8,6 +8,32 @@ import SwiftUI struct CardView: View { + // MARK: - PROPERTIES + + @State private var imageNumber: Int = 1 + @State private var randomNumber: Int = 1 + + // MARK: - FUNCTIONS + + func randomImage() { + print("--- BUTTON WAS PRESSED ---") + print("Status: Old Image Number = \(imageNumber)") + + // Generate a random number until the the generated number is different than the previous one + // This avoids having the same image displayed multiple times in a row + repeat { + randomNumber = Int.random(in: 1...5) + print("Action: Random Number Generated = \(randomNumber)") + + } while randomNumber == imageNumber + + imageNumber = randomNumber + + print("Result: New Image Number = \(imageNumber)") + print("--- THE END ---") + print("\n") + } + var body: some View { // MARK: - CARD @@ -34,7 +60,7 @@ struct CardView: View { ) Spacer() - + Button { // ACTION: Show a sheet print("the button was pressed.") @@ -43,7 +69,7 @@ struct CardView: View { } } - + Text("Fun and enjoyable activity for friends and families.") .multilineTextAlignment(.leading) .italic() @@ -67,12 +93,35 @@ struct CardView: View { ) .frame(width: 256, height: 256) - Image("image-1") + Image("image-\(imageNumber)") .resizable() - .scaledToFit() + .scaledToFit() + .animation(.default, value: imageNumber) } // MARK: - FOOTER + + Button { + // ACTION: Generate a random number + print("Hello") + randomImage() + } label: { + Text("Explore More") + .font(.title2) + .fontWeight(.heavy) + .foregroundStyle( + LinearGradient( + colors: [ + .colorGreenLight, + .colorGreenMedium + ], + startPoint: .top, + endPoint: .bottom + ) + ) + .shadow(color: .black.opacity(0.25), radius: 0.25, x: 1, y: 2) + } + .buttonStyle(GradientButton()) } } //: CARD .frame(width: 320, height: 570) From d7741065acea6c71f43959c8306d7f8082e8a170 Mon Sep 17 00:00:00 2001 From: Dorian <61510923+dodoleouf@users.noreply.github.com> Date: Sun, 23 Jun 2024 23:49:38 +0200 Subject: [PATCH 3/3] =?UTF-8?q?Create:=20GradientButton=20struct=20in=20or?= =?UTF-8?q?der=20to=20customize=20a=20given=20button's=20style=20?= =?UTF-8?q?=F0=9F=91=A8=F0=9F=8F=BB=E2=80=8D=F0=9F=8E=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HikeApp/Utility/GradientButtonStyle.swift | 37 +++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/HikeApp/Utility/GradientButtonStyle.swift b/HikeApp/Utility/GradientButtonStyle.swift index 110e00f..eede11b 100644 --- a/HikeApp/Utility/GradientButtonStyle.swift +++ b/HikeApp/Utility/GradientButtonStyle.swift @@ -6,3 +6,40 @@ // import Foundation +import SwiftUI + +struct GradientButton: ButtonStyle { + func makeBody(configuration: Configuration) -> some View { + configuration + .label + .padding(.vertical) + .padding(.horizontal, 30) + .background( + // Conditional Statement with Nul Coalescing + // Condition ? A : B + configuration.isPressed ? + // A: When user press the button + LinearGradient( + colors: [ + .colorGrayMedium, + .colorGrayLight + ], + startPoint: .top, + endPoint: .bottom + ) + + : + + // B: When the Button is not pressed + LinearGradient( + colors: [ + .colorGrayLight, + .colorGrayMedium + ], + startPoint: .top, + endPoint: .bottom + ) + ) + .clipShape(.rect(cornerRadius: 40)) + } +}