Post

Replies

Boosts

Views

Activity

Button animation bug: repeated movement behaviour
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!
1
0
387
Feb ’24
Unexpected behaviour SwiftUI
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.
2
0
410
Feb ’24
Falling effect SwiftUI
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?
1
0
387
Feb ’24
Error installing iOS 17.2 Simulator Xcode
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.
3
2
2.1k
Jan ’24
Need a help
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?
2
0
920
Jul ’23
URGENT!
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.
0
0
630
Apr ’23
Device’s Orientation
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?
3
0
718
Apr ’23
Something is not working well
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) ) } }
3
0
772
Apr ’23
Error appeared
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?
2
0
1.1k
Dec ’22
No profiles founded
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?
0
0
653
Nov ’22