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!
Post
Replies
Boosts
Views
Activity
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.
Hi everyone!
import SwiftUI
struct ContentView: View {
let screenHeight = UIScreen.main.bounds.height
let letters: [String] = ["A", "B"]
@State private var yOffset: CGFloat = 0.0
var body: some View {
ForEach(letters, id: \.self) { letter in
Text(letter)
.padding()
.font(.largeTitle)
.offset(x: 0, y: yOffset)
.animation(Animation.linear(duration: 5.0).repeatForever(autoreverses: false))
.onAppear {
yOffset = screenHeight - 90
}
}
}
}
I would like in this code, the letters to fall from the beginning of the screen to its end, I have the size of screen's height, but actually I can't understand how I can I use it, until the letters can start from the beginning of the screen. And I would also like that once they reach the bottom, they start falling again...
I can't understand how this effect can be achieved, is there anyone who can help me?
Hi guys!
I want to know how can I create an array of UIImage elements and then access these elements randomly?
I tried several times, but I have this issue:
Can someone help me?
Hello and happy new year to all!
I've updated Xcode but I'm having a problem installing the iOS 17.2 Simulator.
It's the 4th time I'm trying to download it, but it gives me this error:
I've also download watchOS which is successfully installed, but I can't understand why only iOS is having some issues.
If someone knows how to fix this issue, please help me, I'm unable to use Xcode without the iOS simulator :(
Thanks in advance.
Hi there,
I'm studying SwiftUI on my own, but unfortunately I'm still not able to write complex lines of code, or even code an app independently, which is very frustrating for me.
I love coding, it's a way to express my creativity.
I already have a lot of ideas, but I can't put them into practice because I feel like I don't have learned enough about this programming language.
Can any of you suggest me some free courses or other resources that have also helped you in your first steps with coding?
Hello everyone,
I was working on a Xcode playground, and unexpectedly it appeared an error that I can't understand how to fix.
Here's the error that I'm facing.
Does someone know how to fix it?
Thanks in advance ;)
Hi all!
Does anyone know how can I drag and drop on a precise location in SwiftUI?
I mean suppose I have a lots of circles and the circles with same color they must go in the same box.
How can I do it?
Hey guys,
I'm having a huge problem and can't understand how to fix it.
I was trying to recreate an Xcode app project with a Xcode Playground, now the point is that in Xcode Project on the side of the screen there are the options to add files.
Like this:
And in the Playground it is like this:
But here, when I add the Swift Files under Shared Sources, and create the ContentView() in Main - Xcode Playground, it says me that it can't find the Helicopter, Pixel, Obstacle, so it can't find the Shared Recourses' files.
Here's all the code.
ContentView(Main):
import SwiftUI
import PlaygroundSupport
struct ContentView: View {
@State private var heliPosition = CGPoint(x:100, y: 100)
@State private var obstPosition = CGPoint(x:1000, y: 300)
@State private var isPaused = false
@State private var score = 0
@State var timer = Timer.publish(every: 0.1, on: .main, in: .common).autoconnect()
var body: some View {
GeometryReader { geo in
ZStack{
Helicopter()
.position(self.heliPosition)
.onReceive(self.timer) {_ in
self.gravity()
}
Obstacle()
.position(self.obstPosition)
.onReceive(self.timer) {_ in
self.obstMove()
}
Text("\(self.score)")
.foregroundColor(.white)
.position(x: geo.size.width - 100, y: geo.size.height / 10 )
self.isPaused ? Button("Resume") { self.resume() } : nil
}
.frame(width: geo.size.width, height: geo.size.height)
.background(Color.black)
.gesture(
TapGesture()
.onEnded{
withAnimation{
self.heliPosition.y += 100
}
})
.onReceive(self.timer) { _ in
self.collisionDetection();
self.score += 1
}
}
.edgesIgnoringSafeArea(.all)
}
func gravity() {
withAnimation{
self.heliPosition.y -= 20
}
}
func obstMove() {
if self.obstPosition.y > 0
{
withAnimation{
self.obstPosition.y -= 20
}
}
else
{
self.obstPosition.x = 1000
self.obstPosition.y = CGFloat.random(in: 0...500)
}
}
func pause() {
self.timer.upstream.connect().cancel()
}
func resume() {
self.timer = Timer.publish(every: 0.1, on: .main, in: .common).autoconnect()
self.obstPosition.x = 1000
self.heliPosition = CGPoint(x: 100, y: 100)
self.isPaused = false
self.score = 0
}
func collisionDetection() {
if abs(heliPosition.x - obstPosition.x) < (25 + 10) && abs(heliPosition.y - obstPosition.y) < (25 + 100) {
self.pause()
self.isPaused = true
}
}
}
PlaygroundPage.current.setLiveView(ContentView())
Pixel:
import SwiftUI
public struct Pixel: View {
let size: CGFloat
let color: Color
public var body: some View {
Rectangle()
.frame(width: size, height: size)
.foregroundColor(color)
}
}
Helicopter:
import SwiftUI
public struct Helicopter: View {
let rows = 5
let columns = 5
let size: CGFloat = 10
let heliBlocks: [[Color]] = [[.gray,.gray,.gray,.gray,.gray],
[.clear,.clear,.green,.clear,.clear],
[.green,.green,.green,.green,.gray],
[.clear,.clear,.green,.green,.green],
[.clear,.clear,.gray,.clear,.gray]]
public var body: some View {
VStack (spacing: 0) {
ForEach((0...self.rows - 1), id: \.self) { row in
HStack (spacing: 0) {
ForEach((0...self.columns - 1), id: \.self) { col in
VStack (spacing: 0) {
Pixel(size: self.size, color: self.heliBlocks[row][col])
}
}
}
}
}
}
}
Obstacle:
import SwiftUI
public struct Obstacle: View {
let width: CGFloat = 20
let height: CGFloat = 200
public var body: some View {
Rectangle()
.frame(width: width, height: height)
.foregroundColor(Color.green)
}
}
Guys please, if anyone knows how to fix this, please help me.
I will be grateful to you.
Hey there!
Does anyone know how I can have the same view that I have with the vertical orientation also with the horizontal orientation?
Because when I change orientation from the vertical to horizontal it is all upside down, is there a way to fix it?
Hey guys!
I wanted to place the circles along the screen, I took the screen size with UIScreen.main.bounds, so that the circle did not come out of the edges of the screen, and yet it is still coming out! Would any of you be able to help me on how to make the circle stay within the edges of the screen, please?
I'm using SwiftUI and Swift Playgrounds.
struct ContentView: View {
let screenWidth = UIScreen.main.bounds.width
let screenHeight = UIScreen.main.bounds.height
var body: some View {
Circle()
.foregroundColor(.purple)
.frame(width: 30, height: 30)
.position(
x: CGFloat.random(in: 0..<screenWidth),
y: CGFloat.random(in: 0..<screenHeight)
)
}
}
Hello guys!
In SwiftUI is there a function to show different objects in random order or can I just do this by assigning the coordinates to each individual element?
For example, suppose I have a lot of circles, and I want to position them randomly, how can I do it?
Thanks in advance.
Hey guys!
Can someone suggest me a free Application for Mac or iPad to create the 2d assets for my project?
Thanks.
Hi friends!
I was developing an app, everything was working OK, but when I played the app on my iPhone connecting it via cable to the Mac an error has appeared and it says "The operation couldn't be completed. Unable to log in with account ... "
"No profiles for ... were found: Xcode couldn't find any iOS app Development provisioning devices matching ..." and I'm not able to try it on my iPhone.
Any suggestion or idea to fix these errors?
Hi friends!
I was developing an app, everything was working OK, but when I played the app on my iPhone connecting it via cable to the Mac an error has appeared and it says
"The operation couldn't be completed. Unable to log in with account ... "
"No profiles for ... were found: Xcode couldn't find any iOS app Development provisioning devices matching ..." and I'm not able to try it on my iPhone.
Any suggestion or idea to fix these errors?