Hello there,
I want to create letters "rain" effect in SwiftUI, actually in this code, I would like letters to start from the top and once they reach the end of the screen, be randomly placed again all over the X axis at the top of the screen (red part in the picture) and fall linear, but unfortunately these letters make strange movements, and I don't understand why they react this way I tried to change the offset values, but it didn't work at all! :(
import SwiftUI
struct ContentView: View {
let screenWidth = UIScreen.main.bounds.width
let screenHeight = UIScreen.main.bounds.height
let letters: [String] = ["A", "B", "C", "D"]
@State private var yOffset: CGFloat = 0.0
var body: some View {
VStack {
ForEach(letters, id: \.self) { letter in
Text(letter)
.padding()
.font(.largeTitle)
.position(x: CGFloat.random(in: 0...screenWidth))
.offset(x: 0, y: yOffset)
.animation(Animation.linear(duration: 5.0).repeatForever(autoreverses: false))
.onAppear {
yOffset = screenHeight - 90
}
}
Spacer()
}
}
}
Please, If anyone knows how to fix this issue, help me.
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
Hi,
I've created an effect that after clicking on the "Hello, world!" button, it has to move (first move is when the user taps on it), and then it has to move again, but this time without any interaction, and it does. But the problem is that when I click again on it, it goes back and then returns to the same position, I can't understand how to solve this issue.
This is my code:
import SwiftUI
public struct ContentView: View {
@State var xPos: CGFloat = 300
@State var yPos: CGFloat = 400
@State var size: CGFloat = 120
public var body: some View {
ZStack {
Image("background")
.resizable()
.frame(width: 700, height: 500)
.padding()
.overlay {
Button(action: {
xPos = 170
yPos = 310
size = 60
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
xPos = 700
}
}) {
Text("Hello, world!")
.font(.system(size: size))
.position(x: xPos, y: yPos)
}
.animation(.spring)
}
}
}
}
Can anyone help me?
Thanks in advance!