Hello guys! Can I attach one object to another using a magnate animation in SwiftUI? For example In the code below I want the blue square to attach with the red one, when I drag it near it.
Here's. my code:
import PlaygroundSupport
import SwiftUI
struct ContentView: View {
@State private var isDragging = false
@State private var dragOffset: CGSize = .zero
@State var position: CGSize = .zero
@State private var hovered = false
var body: some View {
RoundedRectangle(cornerRadius: 20)
.foregroundColor(.red)
.frame(width: 100, height: 100)
.position(x: 400, y: 350)
RoundedRectangle(cornerRadius: 20)
.foregroundColor(.blue)
.frame(width: 100, height: 100)
.position(x: 400, y: 350)
.animation(.default, value: hovered)
.offset(x: dragOffset.width + position.width, y: dragOffset.height + position.height)
.gesture(
DragGesture()
.onChanged({ value in
self.dragOffset = value.translation
})
.onEnded({ value in
self.position.width += value.translation.width
self.position.height += value.translation.height
self.dragOffset = .zero
})
)
}
}
PlaygroundPage.current.setLiveView(ContentView())
If anyone knows how to do this, please help me.
Thanks
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
Hi there!
Does anyone know if the flight for the Special Event at Apple Park can be paid by Apple?
Anyone who knows how it works, please help me.
Hello developers!
I have a question about which programming language is good for building a website. I searched on the internet and it says that CSS and HTML are the most used, so suppose I want to use CSS, now the question is: where can I code to build the website? For example if I want to code in SwiftUI there are some apps (Xcode and Swift Playgrounds) to do it, but how can I do with the website in CSS?
And what is the WebKit, can I still use it to build web pages using SwiftUI?
If someone has the right answer, please help me.
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?
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.
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 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.
Hello world,
can I draw something else in Xcode, like when I write Circle(), it draws me a circle in the screen, or when I write Rectangle(), it appear a rectangle in the simulator... So are there other commands to draw some different shapes like this or I can elusively use these ones?
Thank you.
Hi guys,
I want to put some space between the content and the top of the screen…
I don’t know where exactly I have to write this command in my code, please help me.
Thank you
Hello everyone,
I would like to a flip animation, so that when I press on the rectangle it will flip to other side (like in a cards game)
How can I do that?
Thank you
Here's my code:
import SwiftUI
import PlaygroundSupport
struct ContentView: View {
var body: some View {
Group {
Button {
} label: {
Rectangle()
.frame(width: 140, height: 170)
.foregroundColor(Color(.systemTeal))
.cornerRadius(20)
}
}
}
}
PlaygroundPage.current.setLiveView(ContentView())
Hello guys, everything was working perfectly but I can't do a thing please help me.
I was trying to put into this Rectangle some text , I've tried several lines of code but I can't put the Text INTO the rectangle. Can someone help me?
Here's the code
import SwiftUI
import PlaygroundSupport
struct ContentView: View {
@State var flipped = false
var body: some View {
Group {
RoundedRectangle(cornerRadius: 20)
.frame(width: 140, height: 170)
.foregroundColor(self.flipped ? .red : .orange)
.padding()
.rotation3DEffect(self.flipped ? Angle(degrees: 180): Angle(degrees: 0), axis: (x: CGFloat(0), y: CGFloat(10), z: CGFloat(0)))
.animation(.default)
.onTapGesture {
self.flipped.toggle()
}
}
}
}
PlaygroundPage.current.setLiveView(ContentView())
Hello there!
I would like to build a playground using Swift Playgrounds in AR. And I want to ask if it is possible to do it on the Mac or I can only do it with the iPad?
Thank you.
Hello there! I would like to move the object according to the button press. In this code, I have the circle and I like to move it down if I press the down button. Can anyone tell me how can I do this?
Thanks.
import SwiftUI
import PlaygroundSupport
struct ContentView: View {
var body: some View {
Circle()
.frame(width: 80, height: 80)
Spacer(minLength: 20)
Button{
} label: {
Image(systemName: "arrowtriangle.down.circle.fill")
.resizable()
.foregroundColor(.blue)
.frame(width: 70, height: 70)
.padding()
}
}
}
PlaygroundPage.current.setLiveView(ContentView())
Hello there! I would like to build a game in SwiftUI where you can move an object with your mouse (for example: a rectangle points your mouse, and it goes where your mouse goes).
How can I do it?
Hello everyone,
I'm trying to make particle effect in SwiftUI, following a YouTube tutorial and this is what I made, despite I wrote the same code as the video, it gives me this error (which doesn't appear in the video). So can anyone tell me what I did wrong?
Here's my code:
And the link for the tutorial:
https://www.youtube.com/watch?v=oj4HEqkDvBY
import SwiftUI
import PlaygroundSupport
struct ContentView: View {
var body: some View {
Text("Hello, world!")
}
}
struct ParticleEffect: ViewModifier {
let count: Int
let duration: Double = 2.0
@State var time: Double = 0.0
func body(content: Content) -> some View {
let animation = Animation.linear(duration: duration)
.repeatForever(autoreverses: false)
return ZStack {
ForEach(0..<count) { index in
content
.scaleEffect(CGFloat((duration-self.time)/duration))
.modifier(ParticleMotion(time: self.time)) // ERROR: Cannot find 'ParticleMotion' in scope
.opacity((duration-self.time)/duration)
.animation(animation.delay(Double.random(in: 0..<self.duration)))
.blendMode(.plusLighter)
}
.onAppear {
withAnimation() {
self.time = duration
}
}
}
}
}
PlaygroundPage.current.setLiveView(ContentView())