Post

Replies

Boosts

Views

Activity

Reply to Problems with Swift Playgrounds
OK. I tried this: import SwiftUI public struct Block: View {     public var body: some View {         Rectangle()             .frame(width: 90, height: 90)     } } and in the ContentView: But now it gives me one other error. import PlaygroundSupport import SwiftUI struct ContentView: View {     var body: some View {         Block() // ERROR: 'Block' initializer is inaccessible due to 'internal' protection level.     } } PlaygroundPage.current.setLiveView(ContentView())
Apr ’22
Reply to Attach magnetically an object in SwiftUI
Here's the 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()) I want the blue square to attach with the red square when I drag near it.
Mar ’22
Reply to Show a badge on a Rectangle
I tried it but it shows me only the number and I wanted the badge icon, so a circle with a number on it. I tried this but the circle is very big how can I set the dimension of the circle so that it looks like a badge? struct ContentView: View {      func increment() {         count = count + 1     }     @State var showBadge = false     @State var count = 0     var body: some View {         Button(action: {             showBadge.toggle() // badge(9)             if showBadge { increment() }         }, label: {             Text("show badge")         })         if showBadge {             Rectangle()                 .frame(width: 180, height: 200)                 .foregroundColor(.blue)                 .position(x: 70, y: 70)                 .badge(count)                 .overlay(                     Circle(), alignment: .topLeading)         } else {             Rectangle()                 .frame(width: 180, height: 200)                 .foregroundColor(.blue)         }} }
Feb ’22
Reply to Add badge to an object
Here's my code: I want to show the badge on the badge on the rectangle each time I press on it. But currently it isn't working what I did wrong? 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())
Feb ’22
Reply to Shuffle randomly in SwiftUI
Okay, but the problem is where to locate them. I did something like this, but it gives errors. import SwiftUI import PlaygroundSupport struct ContentView: View {     var cards = [1, 2, 3, 4, 5, 6]     var body: some View {         cards.shuffle()         print(cards)     } } PlaygroundPage.current.setLiveView(ContentView())
Feb ’22
Reply to Learn to code
Hi! When I started learn coding with Swift Playgrounds, I remember some levels were not easy to me, in fact I stayed on them many days. If you want to learn how code in Swift Playgrounds I recommend you the books about coding in Swift, you can find them in "Everyone Can Code" series on Apple Books and they're free. But they'll help you to get started and learn code not to pass levels. I hope it helped you.
Feb ’22