Hello there!
Does anyone know how to apply Apple's default fonts to text in SwiftUI?
Post
Replies
Boosts
Views
Activity
Hi there!
I want to attach 2 objects in SwiftUI when they'll near.
import SwiftUI
struct ContentView: View {
@State private var dragOffset: CGSize = .zero
@State private var position: CGSize = .zero
var body: some View{
Circle()
.frame(width: 140, height: 140)
.foregroundColor(.green)
.position(x: 150, y: 90)
.offset(x: dragOffset.width + position.width, y: dragOffset.height)
.gesture(DragGesture()
.onChanged({ (value) in
self.dragOffset = value.translation
})
)
Circle()
.frame(width: 140, height: 140)
.foregroundColor(.blue)
.position(x: 500, y: 210)
Circle()
.frame(width: 140, height: 140)
.foregroundColor(.orange)
.position(x: 90, y: 70)
}
}
In my code I want to stick the green circle with one of the other 2, when it is close to them.
Can anyone help me figure out this?
Thanks in advance.
Hello everyone!
I'm having a problem with my code and I don't know how to fix it. In fact, when I click the green circle, it gets bigger by wiping out the others, but the purple overlaps. Is there a way to get the purple away too?
Another problem: I have a warning where it says .animation(.spring()) and the warning says "'animation' was deprecated in iOS 15.0: Use withAnimation or animation(_:value:) instead."
import SwiftUI
struct ContentView: View {
var body: some View {
LazyVGrid(columns: [GridItem(), GridItem(), GridItem()]) {
createCircle()
.foregroundColor(.blue)
createCircle()
.foregroundColor(.green)
createCircle()
.foregroundColor(.purple)
createCircle()
.foregroundColor(.orange)
createCircle()
.foregroundColor(.yellow)
}
}
struct createCircle: View {
@State var scale : CGFloat = 0.25
var body: some View {
ZStack {
Circle()
.frame(width: 500 * scale, height: 500 * scale)
.onTapGesture {
scale = 5
}
.animation(.spring()) // WARNING: 'animation' was deprecated in iOS 15.0: Use withAnimation or animation(_:value:) instead.
.animation(.interpolatingSpring(stiffness: 50, damping: 1), value: scale)
}
}
}
}
Can anyone help me?
Hi there! I'm trying to build something in SwiftUI and I need a help on it. I want the orange circle to scale and expand, and fill the entire screen, whenI click on it.
Here's my code:
import SwiftUI
struct ContentView: View {
var body: some View {
Circle()
.frame(width: 120, height: 120)
.foregroundColor(.orange)
}
}
Thank you.
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
Hello everyone! 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.
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())
Thanks.
Hello everyone! Can I attach an object with a magnete animation to the other in SwiftUI?
Please show me that with a simple example.
Thank you in advance.
Hello everyone!
Can someone tell me where to put animation when I use LazyVGrid, please?
Here’s my code:
struct MyEmoji : Hashable {
var char: String
var num: Int
}
struct ContentView39: View {
var emojis : [MyEmoji] = [MyEmoji(char: "🐶", num: 0), MyEmoji(char: "🐱", num: 1), MyEmoji(char: "🐱", num: 2), MyEmoji(char: "🦊", num: 3), MyEmoji(char: "🦁", num: 4), MyEmoji(char: "🐝", num: 5), MyEmoji(char: "🐼", num: 6), MyEmoji(char: "🐷", num: 7), MyEmoji(char: "🐮", num: 8)]
var body: some View {
LazyVGrid(columns: [GridItem(), GridItem(), GridItem()]) {
ForEach(emojis, id: \.self, content: { emoji in
emojiView(content: emoji.char)
})
}
}
Thanks in advance.
Hello there! I'm trying to create something in SwiftUI, but there's a problem: I can't have 2 cards with the same content, does anyone know a way to have the same emojis in 2 separate cards?
Here's my code:
import PlaygroundSupport
import SwiftUI
struct ContentView: View {
var emojis: [String] = ["🐶", "🐱", "🐱", "🦊", "🦁", "🐝", "🐼", "🐷", "🐮"]
var body: some View {
LazyVGrid(columns: [GridItem(), GridItem(), GridItem()]) {
ForEach(emojis, id: \.self, content: { emoji in
emojiView(content: emoji)
})
}
}
struct emojiView: View {
var content: String = ""
var body: some View {
ZStack {
RoundedRectangle(cornerRadius: 20)
.frame(width: 90, height: 120)
.foregroundColor(.yellow)
Text(content).font(.largeTitle)
}
}
}
}
PlaygroundPage.current.setLiveView(ContentView())
Hi everyone,
I want to try my Xcode project on my iPhone, but I can't because of an error, which says "Unable to prepare iPhone for development". Can someone help me to try my app on my iPhone and fix this error?
Thank you!
Hello guys,
Is it possible to display different overlays on a text in SwiftUI? If someone knows how to do it, please help me with an example. I am in trouble with it.
Thank you very much!
Hello guys,
Is it possible to display different overlays on a text in SwiftUI?
If someone knows how to do it, please help me with an example. I am in trouble with it.
Thank you very much!
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())
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 guys, I want to show a badge on an object in SwiftUI, precisely I want the rectangle to show the badge on it every time I press it. I did some tests, but it isn't working. Can someone help me to build this?
Here’s my code:
import SwiftUI
import PlaygroundSupport
struct ContentView: View {
var body: some View {
Button(action: {
badge(9)
}, label: {
Rectangle()
.frame(width: 180, height: 200)
.foregroundColor(.blue)
})
}
}
PlaygroundPage.current.setLiveView(ContentView())
Thanks a lot.