*BONUS ENTRY* SwiftUI Mini Project - Part 1

A whole week! Not good! I have my reasons - busy weekend and another inspection...plus the run up to the end of term...along with other things. So first chance to code in a while! Today, I am going to spend around an hour just trying out SwiftUI with a very simple mini-project - F1 driver facts. This is not to try making an app to sell or anything long term. Just something that is functional and lets me try out some SwiftUI features. 

Start Time - 19:12

First of all, I need to map out the purpose of the project and what I need - what features of SwiftUI.

Purpose - to find out about F1 drivers.

Features:

Pictures of drivers (standard size)
Toggle view to swipe between them
Information about the drivers - championships, races, wins, mini bio

OK what is needed for this simple project?

Class of driver so all the info is contained together
Pictures (need to get all portrait)
Info (can look up)
Toggle view and a H Stack of these.
Loop (for each)

How to do this? Well first step is to check back to the project I did before from Laurie's Course...


Paused at 19:21 (saving images and resizing so that they are all same size)

Restart at 20:27

OK all images resized, then done as the x1, x2, x3 etc. Good!

Now to create the Struct for the driver pics and details....

OK, using the book struct from before, I've adapted that for F1 drivers...

import SwiftUI

struct Driver: Identifiable {
  var id = UUID()
  var name: String
  var imageName: String
    var wins: Int
    var championships: Int
}

extension Driver {
  static let f1Drivers = [
    Driver(name: "Alain Prost", imageName: "AP", wins: 51, championships: 4),
    Driver(name: "Ayrton Senna", imageName: "AS", wins: 41, championships: 3),
    Driver(name: "Damon Hill", imageName: "DH", wins: 22, championships: 1),
    Driver(name: "Fernando Alonso", imageName: "FA", wins: 32, championships: 2),
    Driver(name: "Felipe Massa", imageName: "FM", wins: 11, championships: 0),
    Driver(name: "Kimi Raikkonen", imageName: "KR", wins: 21, championships: 1),
    Driver(name: "Mika Hakkinen", imageName: "MH", wins: 20, championships: 2),
    Driver(name: "Michael Schumacher", imageName: "MS", wins: 91, championships: 7),
    Driver(name: "Sebastian Vettel", imageName: "SV", wins: 53, championships: 4)
  ]

}

Next step is to make the toggle view on the main content view...

Right! So using ideas from Laurie (of course) here's what we have...

import SwiftUI

struct ContentView: View {
    var body: some View {
        ScrollView(.horizontal, showsIndicators: false) {
            
            HStack {
                ForEach(Driver.f1Drivers) {  driver in
                    
                    GeometryReader { proxy in
                                           
                                           VStack {
                           Text(driver.name)
                                            Image(driver.imageName)
                        }
                    
                }
            }
            
            
        }
    }
}
    
}


But it's come up a mess!!


Let's just get the basics right with the properties, then that will do for now!

Right so I needed ALL of this code, along with a couple of other properties .resizable and another for the image. 

It is:

 .frame(width: max(proxy.size.width - proxy.frame (in: .global).midX, proxy.size.width) , height: proxy.size.height - 50)
                                                .background(
                                                    Image(driver.imageName)
                                                        
                                                        .resizable()
                                                        .scaledToFill()
                                                        .overlay(Color.blue)
                                                        
                                                        .blendMode(.multiply)
                                                        .blur(radius: 1)
                                                    
                                                )
                                                    
                                                    .cornerRadius(10)
                                                    .padding()
                                                    .shadow(radius: 3)
                                                    
                                                    .layoutPriority(1)
                                                    
                                                    .rotation3DEffect(Angle(degrees: Double(proxy.frame(in: .global).midX) / 40), axis: (x: -4, y: -3, z: -3))
                                            }
                                            .frame(width: 200, height: 300)
                                        }
                                    }
                                }
                                    
                                Spacer()
                                    .layoutPriority(1)
                                    
                            }
                                .background(Color.black)
                                .edgesIgnoringSafeArea(.all)
                             

Massive! But I will delve into all that a bit more next time. For now we've got a pretty cool thing!




Even better!

Finish Time - 21:05 (approx 45 minutes)

So most of that was copying and pasting to be honest. But it's good! It's just adapting some code to try to understand each aspect. Next time I will delve deeper and play around with the properties. Then the final step will be to add in the navigation bar bit, then bring up some info on each driver. 

Good experimenting!


Comments

Popular posts from this blog

*Xcode Project Entry 2* F1 Quiz - part 1

Angela Yu Course Part 10 (up to lesson 112)

Angela Yu Xcode 12 Course - Part 7 (lectures 74 to 79)