Requested
Considered
265 instances
I’ve simply began wanting into SwiftUI and since it is so completely different I am attempting to wrap my head round primary ideas.
On this state of affairs, how would I am going about altering the colour just for the circle tapped?
ForEach(1...depend, id: .self) { _ in
Circle()
.foregroundColor(colours.randomElement()).opacity(0.2)
.body(width: .random(in: 10...100), peak: .random(in: 10...100))
.place(x: .random(in: self.stepperValue...400),
y: .random(in: self.stepperValue...400))
.saturation(2.0)
.onTapGesture(depend: 1, carry out: {
// Change colour of circle that was tapped
print("Tapped")
})
.animation(.default) // Animate the change in place
}
You create a View
that has the “Rows” particular person properties
import SwiftUI
struct SampleColorChangeView: View {
//If the choices are mounted no must control them
//You may transfer this all the way down to the Row in the event you needn't have them obtainable right here
let colours: [Color] = [.red,.blue,.gray, .yellow,.orange]
@State var depend: Int = 10
var physique: some View {
VStack{
ForEach(1...depend, id: .self) { _ in
RowView(colours: colours)
}
}
}
}
//Create a row View to look at particular person objects
//You'll do that with something that you simply need to Observe independently
struct RowView: View {
let colours: [Color]
//@State observes adjustments so the View is up to date
@State var colour: Colour = .blue
//This sort of works like the colours would you like one for every or a shared for all. Does the dad or mum want entry? You may transfer it up or maintain it right here
@State var stepperValue: CGFloat = 0
//The one change right here is the reference to the person Colour
var physique: some View {
Circle()
//You set the person colour right here
.foregroundColor(colour).opacity(0.2)
.body(width: .random(in: 10...100), peak: .random(in: 10...100))
.place(x: .random(in: self.stepperValue...400),
y: .random(in: self.stepperValue...400))
.saturation(2.0)
.onTapGesture(depend: 1, carry out: {
// Change colour of circle that was tapped
colour = colours.randomElement()!
print("Tapped")
})
.animation(.default) // Animate the change in place
//If you wish to set a random colour to begin vs simply having all of them be the identical Colour you are able to do one thing like this
.onAppear(){
colour = colours.randomElement()!
}
}
}
struct SampleColorChangeView_Previews: PreviewProvider {
static var previews: some View {
SampleColorChangeView()
}
}
Effectively there are two predominant choices I see right here
- Make a customized view like
struct MyCircle: View {
@State var colour: Colour?
var physique: some View {
Circle()
.foregroundColor(colour)
.onTapGesture {
self.colour = colours.randomElement()
}
}
}
after which combine that or
- Use a mannequin in your colour
struct MyView: View {
@State var colours = allColors.indices.compactMap { _ in allColors.randomElement() }
var physique: some View {
ForEach(colours.indices) { index in
Circle()
.foregroundColor(colours[index])
.onTapGesture {
colours[index] = allColors.randomElement()
}
}
}
}
A state like this could ideally be in its personal class which needs to be inserted as ObservedObject
.
2
lang-swift