Im making an app that uses drag&drop but, it doesn't work.
This is made with Swift Playground.
If you can find a solution, please let me know.
Here is the code.
//MARK: Drop Area
@ViewBuilder
func DropAera()->some View{
VStack(spacing: 12){
ForEach($rows,id:\.self){$row in
HStack(spacing: 10){
ForEach($row){$item in
Text(item.value)
.font(.system(size: item.fontSize))
.padding(.vertical,5)
.padding(.horizontal,item.padding)
.opacity(item.isShowing ? 1 : 0)
.background{
RoundedRectangle(cornerRadius: 6, style: .continuous)
.fill(item.isShowing ? .clear : .gray.opacity(0.25))
}
.background{
// If Item is Dropped into Correct Plase
RoundedRectangle(cornerRadius: 6, style: .continuous)
.stroke(.gray)
.opacity(item.isShowing ? 1 : 0)
}
// MARK: Adding Drop Operation
// MARK: Adding Drag Drop Operation
.onDrop(of: [.url], isTargeted: .constant(false)) {
providers in
if let first = providers.first{
let _ = first.loadObject(ofClass: URL.self) {
value,error in
guard let url = value else{return}
if item.id == "\(url)"{
droppedCount += 1
let progress = (droppedCount / CGFloat(characters.count))
withAnimation{
item.isShowing = true
updateShuffledArray(character: item)
self.progress = progress
}
}
else{
//Animating When Wrong text
animateView()
}
}
}
return false
}
}
}
if rows.last != row{
Divider()
}
}
}
}
@ViewBuilder
func DragArea()->some View{
VStack(spacing: 12){
ForEach(shuffledRows,id: \.self){row in
HStack(spacing: 10){
ForEach(row){item in
Text(item.value)
.font(.system(size: item.fontSize))
.padding(.vertical,5)
.padding(.horizontal,item.padding)
.background{
RoundedRectangle(cornerRadius: 6, style: .continuous)
.stroke(.gray)
}
// MARK: Adding Drag Drop Operation
.onDrag{
// Returning ID to find whitch Item is Moving
return .init(contentsOf: URL(string: item.id))!
}
.opacity(item.isShowing ? 0 : 1)
.background{
RoundedRectangle(cornerRadius: 6, style: .continuous)
.fill(item.isShowing ? .gray.opacity(0.25) : .clear)
}
}
}
if shuffledRows.last != row{
Divider()
}
}
}
}
Swift Playgrounds
RSS for tagLearn and explore coding in Swift through interactive learning experiences on the Swift Playgrounds app for iPadOS and macOS.
Posts under Swift Playgrounds tag
94 Posts
Sort by:
Post
Replies
Boosts
Views
Activity
I have a CollectionView in my SwiftUI App. The collectionView is wrapped in a UIViewRepresentable. If I tap on a collectionView cell normally the function isn't called but if I hold the cell down the console prints <0x107507790> Gesture: System gesture gate timed out. and when I let go of the cell after that's printed then didSelectItem is called.
Here is my CollectionView Class:
class SessionsCollectionView: UIView, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
lazy var collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 30
let collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.dataSource = self
collectionView.delegate = self
collectionView.clipsToBounds = false
collectionView.delaysContentTouches = false
collectionView.register(SessionCell.self, forCellWithReuseIdentifier: "cell")
collectionView.register(SessionsHeader.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "header")
return collectionView
}()
override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = .clear
setupCollectionView()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func setupCollectionView() {
addSubview(collectionView)
collectionView.backgroundColor = .clear
collectionView.alwaysBounceVertical = true
collectionView.alwaysBounceHorizontal = false
collectionView.delaysContentTouches = false
collectionView.contentInset = .init(top: 20, left: 0, bottom: 20, right: 0)
collectionView.showsVerticalScrollIndicator = false
NSLayoutConstraint.activate([
collectionView.topAnchor.constraint(equalTo: topAnchor),
collectionView.leadingAnchor.constraint(equalTo: leadingAnchor),
collectionView.trailingAnchor.constraint(equalTo: trailingAnchor),
collectionView.bottomAnchor.constraint(equalTo: bottomAnchor)
])
}
// MARK: Header
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
if kind == UICollectionView.elementKindSectionHeader {
let sectionHeader = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "header", for: indexPath) as! Header
return sectionHeader
}
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
let width: CGFloat = collectionView.frame.width
let height: CGFloat = 33 + 20
return CGSize(width: width, height: height)
}
// MARK: - UICollectionViewDataSource
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! SessionCell
return cell
}
// MARK: - UICollectionViewDelegateFlowLayout
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let width = collectionView.bounds.width
let height = 150
return CGSize(width: collectionView.bounds.width, height: height)
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
print(indexPath.row)
}
}
struct SessionsCollectionViewWrapper: UIViewRepresentable {
var sessionViewNavigation: SessionViewNavigation
func makeUIView(context: Context) -> SessionsCollectionView {
let sessionsCollectionView = SessionsCollectionView()
return sessionsCollectionView
}
func updateUIView(_ uiView: SessionsCollectionView, context: Context) {
// Update any properties or handle updates if needed
}
}
Hello,
For the Swift Student Challenge, do I need to worry about submitting a clean source code with comments? Or does Apple judge submissions solely based on their user experience with the app?
Thanks!
I have been attempting to debug this for over 10 hours...
I am working on implementing Apple's MobileNetV2 CoreML model into a Swift Playgrounds. I performed the following steps
Compiled CoreML model in regular Xcode project
Moved Compiled CoreML (MobileNetV2.mlmodelc) model to Resources folder of Swift Playground
Copy Paste the model class (MobileNetV2.swift) into the Sources folder of Swift Playground
Use UIImage extensions to resize and convert UIImage into CVbuffer
Implement basic code to run the model.
However, every time I run this, it keeps giving me this error:
MobileNetV2.swift:100: Fatal error: Unexpectedly found nil while unwrapping an Optional value
From the automatically generated model class function:
/// URL of model assuming it was installed in the same bundle as this class
class var urlOfModelInThisBundle : URL {
let bundle = Bundle(for: self)
return bundle.url(forResource: "MobileNetV2", withExtension:"mlmodelc")!
}
The model builds perfectly, this is my contentView Code:
import SwiftUI
struct ContentView: View {
func test() -> String{
// 1. Load the image from the 'Resources' folder.
let newImage = UIImage(named: "img")
// 2. Resize the image to the required input dimension of the Core ML model
// Method from UIImage+Extension.swift
let newSize = CGSize(width: 224, height: 224)
guard let resizedImage = newImage?.resizeImageTo(size: newSize) else {
fatalError("⚠️ The image could not be found or resized.")
}
// 3. Convert the resized image to CVPixelBuffer as it is the required input
// type of the Core ML model. Method from UIImage+Extension.swift
guard let convertedImage = resizedImage.convertToBuffer() else {
fatalError("⚠️ The image could not be converted to CVPixelBugger")
}
// 1. Create the ML model instance from the model class in the 'Sources' folder
let mlModel = MobileNetV2()
// 2. Get the prediction output
guard let prediction = try? mlModel.prediction(image: convertedImage) else {
fatalError("⚠️ The model could not return a prediction")
}
// 3. Checking the results of the prediction
let mostLikelyImageCategory = prediction.classLabel
let probabilityOfEachCategory = prediction.classLabelProbs
var highestProbability: Double {
let probabilty = probabilityOfEachCategory[mostLikelyImageCategory] ?? 0.0
let roundedProbability = (probabilty * 100).rounded(.toNearestOrEven)
return roundedProbability
}
return("\(mostLikelyImageCategory): \(highestProbability)%")
}
var body: some View {
VStack {
let _ = print(test())
Image(systemName: "globe")
.imageScale(.large)
.foregroundColor(.accentColor)
Text("Hello, world!")
Image(uiImage: UIImage(named: "img")!)
}
}
}
Upon printing my bundle contents, I get these:
["_CodeSignature", "metadata.json", "__PlaceholderAppIcon76x76@2x~ipad.png", "Info.plist", "__PlaceholderAppIcon60x60@2x.png", "coremldata.bin", "{App Name}", "PkgInfo", "Assets.car", "embedded.mobileprovision"]
Anything would help 🙏
For additional reference, here are my UIImage extensions in ExtImage.swift:
//Huge thanks to @mprecke on github for these UIImage extension function.
import Foundation
import UIKit
extension UIImage {
func resizeImageTo(size: CGSize) -> UIImage? {
UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
self.draw(in: CGRect(origin: CGPoint.zero, size: size))
let resizedImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return resizedImage
}
func convertToBuffer() -> CVPixelBuffer? {
let attributes = [
kCVPixelBufferCGImageCompatibilityKey: kCFBooleanTrue,
kCVPixelBufferCGBitmapContextCompatibilityKey: kCFBooleanTrue
] as CFDictionary
var pixelBuffer: CVPixelBuffer?
let status = CVPixelBufferCreate(
kCFAllocatorDefault, Int(self.size.width),
Int(self.size.height),
kCVPixelFormatType_32ARGB,
attributes,
&pixelBuffer)
guard (status == kCVReturnSuccess) else {
return nil
}
CVPixelBufferLockBaseAddress(pixelBuffer!, CVPixelBufferLockFlags(rawValue: 0))
let pixelData = CVPixelBufferGetBaseAddress(pixelBuffer!)
let rgbColorSpace = CGColorSpaceCreateDeviceRGB()
let context = CGContext(
data: pixelData,
width: Int(self.size.width),
height: Int(self.size.height),
bitsPerComponent: 8,
bytesPerRow: CVPixelBufferGetBytesPerRow(pixelBuffer!),
space: rgbColorSpace,
bitmapInfo: CGImageAlphaInfo.noneSkipFirst.rawValue)
context?.translateBy(x: 0, y: self.size.height)
context?.scaleBy(x: 1.0, y: -1.0)
UIGraphicsPushContext(context!)
self.draw(in: CGRect(x: 0, y: 0, width: self.size.width, height: self.size.height))
UIGraphicsPopContext()
CVPixelBufferUnlockBaseAddress(pixelBuffer!, CVPixelBufferLockFlags(rawValue: 0))
return pixelBuffer
}
}
Hi Developers,
I want to create a Vision app on Swift Playgrounds on iPad. However, Vision does not properly function on Swift Playgrounds on iPad or Xcode Playgrounds. The Vision code only works on a normal Xcode Project.
SO can I submit my Swift Student Challenge 2024 Application as a normal Xcode Project rather than Xcode Playgrounds or Swift Playgrounds File.
Thanks :)
Can anyone tell me how to use Swift Data and Core Data with playground?
Hello,
Does anyone know how to restrict my app playground to run on iPhones only (not even iPads) and to always be presented in portrait mode? I'm using Xcode.
Thanks for any help!
Hello,
The appearance of one of my buttons is not updating after being tapped. I have had no trouble doing the exact same thing in my other views, but here it simply doesn't work. The "heart" button will keep its original appearance (in this case an unfilled heart) no matter what, despite my print statements indicating that the value has changed. The other actions performed when tapping the heart work perfectly. I've been at it for hours.
Any help would be appreciated. Thanks!
import SwiftUI
struct SearchView: View {
@State private var searchText = ""
@StateObject var save = SaveWords()
@State var heart: String?
@State var disappear = false
@State var done = true
var body: some View {
NavigationView {
VStack {
if !disappear {
SearchBarView(text: $searchText)
Spacer()
}
if searchText != "" {
List(.constant(Array(FetchWord.getWordFromStart(start: searchText)).prefix(10).map {Word(word: $0.1)})) { suggestion in
NavigationLink(destination: suggestion.IPA.wrappedValue == "error" ? AnyView(EmptyView()) : AnyView(PracticeView(wordSheet: suggestion)
.onAppear {
disappear = true
if done {
SaveWords.file = "Favorites"
DispatchQueue.main.async {
Task {
try? await save.load()
heart = save.words.contains(suggestion.wrappedValue) ? ".fill" : ""
}
}
}
}
.onDisappear {
disappear = false
Task {
SaveWords.file = "History"
try? await save.load()
if save.words.first != suggestion.wrappedValue {
save.words.insert(suggestion.wrappedValue, at: 0)
try? await save.save()
}
}
}
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
Button(action: {
SaveWords.file = "Favorites"
done = false
if heart == "" {
heart = ".fill"
DispatchQueue.main.async {
Task {
try? await save.load()
if !save.words.contains(suggestion.wrappedValue) {
save.words.insert(suggestion.wrappedValue, at: 0)
try? await save.save()
}
}
}
} else {
heart = ""
DispatchQueue.main.async {
Task {
try? await save.load()
try? await save.delete(wordToDelete: suggestion.wrappedValue)
try? await save.save()
}
}
}
done = true
}, label: {
Image(systemName: "heart" + (heart ?? ""))
})
}
})
) {
if suggestion.IPA.wrappedValue != "error" {
CardView(wordSheet: suggestion)
}
}
}
}
}
Spacer()
}
}
}
#Preview {
SearchView()
}
Your app playground must be built with and run on Swift Playgrounds 4.4 or later (requires iPadOS 16 or macOS 13.5, or later) or Xcode 15 on macOS 13.5, or later. You may incorporate the use of Apple Pencil.
If I want to develop a playground for the iPad and Apple Pencil, am I limited to using Playgrounds on iPadOS to make it? Or could I also use Playgrounds on macOS and Xcode?
If I use Xcode to make my iPad playground, should I select "Xcode" for "Which software should we use to run your app playground?" I assume so, as I used Xcode to make my app — but then it will be run in a simulator by the judges, which means they cannot use the Apple Pencil.
I'm a little lost here. Any guidance is appreciated.
I am making a code that uses pencilKit and you can draw on the canvas. However, I want to be able to detect if the resulted drawing is in contact with a Rectangle().
Is there any way to do this?
I dont want to use variables for X and Y positions because I have more than 400 rectangles in a grid.
Hey Developers,
I'm on the hunt for a new Apple laptop geared towards coding, and I'd love to tap into your collective wisdom. If you have recommendations or personal experiences with a specific model that excels in the coding realm, please share your insights. Looking for optimal performance and a seamless coding experience.
Your input is gold – thanks a bunch!
Is it possible to create SwiftData Document-based apps in App Playgrounds (or Swift Packages, since App Playgrounds are a special type of packages)?
As explained in wwdc2023-10154, to create a document-based app you are required to provide an UTType that conforms to com.apple.package. This requires your file type to be declared and exported in the Info.plist file of an Xcode project, but App Playgrounds don't have them.
Providing .package as a UTType seems to partially work:
import SwiftUI
import SwiftData
@main
struct SwiftDataFlashCardSample: App {
var body: some Scene {
#if os(iOS) || os(macOS)
DocumentGroup(editing: Card.self, contentType: .package) {
ContentView()
}
#else
WindowGroup {
ContentView()
}
.modelContainer(for: Card.self)
#endif
}
}
This way I am able to run the app, create and edit new document, and save it on disk. However, opening it again does not work (you cannot select it since it is a folder without a defined package type).
Is there any workaround to this?
I guess I could drop support for multiple documents entirely if this is not possible (I don't think this would affect my submission), but I would just like to check if there is any way to do this first.
the code found in the file "Camera" in the template for "Capturing Photos" gives warnings that say
How can I go around this by still keeping the same functions as the original "Camera" file? I am using a features that REQUIRES Mac Catalyst 17.0 or above.
Hi everyone,
I just finished my first app and I have a doubt about whether picker view automatically makes a series of loops in its func.
I put some prints inside the funcs and this is the result only when starting the view with the picker:
Every time I select it the cycles continue.
It's normal? Is there a way to avoid or control it?
Thanks in advance for any response.
Hello,
Very recently, the following code has automatically appeared at the bottom of three of my SwiftUI View files:
@available(iOS 17.0, macOS 14.0, tvOS 17.0, visionOS 1.0, watchOS 10.0, *)
struct $s10Accent_Ace33_0BADA584A03144EFDAB57154E6FD3FBALl7PreviewfMf_15PreviewRegistryfMu_: DeveloperToolsSupport.PreviewRegistry {
static let fileID: String = "Accent_Ace/HistoryView.swift"
static let line: Int = 47
static let column: Int = 1
static func makePreview() throws -> DeveloperToolsSupport.Preview {
DeveloperToolsSupport.Preview {
let randomWord1 = FetchWord.getRandomWord()
let randomWord2 = FetchWord.getRandomWord()
@State var randomWords = [Word(word: randomWord1.0, IPA: randomWord1.1, lineNumber: randomWord1.2), Word(word: randomWord2.0, IPA: randomWord2.1, lineNumber: randomWord2.2)]
HistoryView(words: $randomWords)
}
}
}
This is from one of my files but it's very similar in the other two. It seems to have something to do with my previews.
The problem is that this code generates an error: Ambiguous use of 'init(_:traits:body:)'. My previews worked just fine before the auto-generated code appeared, so I tried deleting it. However, it automatically comes back no matter how many times I get rid of it. It's preventing me from building my App Playground, so I can't run the app or even see the previews. Does anyone know how to get rid of it or fix the error?
Thanks for the help!
Hi there, I just started with swift and my playground is getting stuck whenever i am trying to execute something, it ain't showing any error but just stuck.
i have a simple code running on Xcode 15.2 and is showing blank in the liveView
I am expecting to see Hello button in default blue , which navigates me to empty screen when tapped
can someone helpe me why preview is not rendering properly?
thank you so much
Hello, is the new Journaling Suggestions API available in App Playgrounds (made using Xcode)?
I know I can add capabilities in Xcode, but it seems that the Journaling Suggestions capability isn't available. I assume this API is currently unsupported in App Playgrounds?
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,
My App Playground uses a text file to work properly. I've had no issues running the app on Simulator, but it fails to find the file when being run on my physical iPhone. Has anyone had the same issue? Any idea what the problem is?
Thanks for the help!