Post

Replies

Boosts

Views

Created

nested loadObject call issue
I write some code to show an image dragged from other apps(such as web browser, photos, etc). I make a delegate to perform the drop. If DropInfo has an image item, I will try to retrieve the data as uiimage by NSItemProvider.loadObject first. If errors occur during loading, I will ask DropInfo again whether it has a url item. If the answer is YES, I will try to retrieve URL by NSItemProvider.loadObject. It means the second loadObject will be nested in the first one. When running the simulator, I find the second loadObject completion handler is never called which is supposed to be called when I try to retrieve URL. Do I miss anything? import SwiftUI import Combine struct ContentView: View{ @State private var img: UIImage? var body: some View{ Image(uiImage: img != nil ? img! : UIImage(systemName: "photo")!) .resizable() .scaledToFit() .onDrop(of: [.image], delegate: ImageDropController(img: $img)) } } class ImageDropController: DropDelegate{ @Binding var img: UIImage? init(img: Binding<UIImage?>) { _img = img } func performDrop(info: DropInfo) -> Bool { if getImgFromImage(info: info){ return true }else{ return false } } func getImgFromImage(info: DropInfo) -> Bool{ guard info.hasItemsConforming(to: [.image]) else { return getImgFromUrl(info: info) } createImgFromImage(from: info.itemProviders(for: [.image]).first!,info: info) return true } func createImgFromImage(from provider: NSItemProvider, info: DropInfo){ provider.loadObject(ofClass: UIImage.self) { image, error in var unwrappedImage: UIImage? if let error = error { print("unwrapImage failed: ", error.localizedDescription) _ = self.getImgFromUrl(info: info) } else { unwrappedImage = image as? UIImage } if let image = unwrappedImage{ DispatchQueue.main.async { self.img = image } } } } func getImgFromUrl(info: DropInfo) -> Bool{ guard info.hasItemsConforming(to: [.url]) else { return false } createImgFromUrl(from: info.itemProviders(for: [.url]).first!) return true } private func createImgFromUrl(from provider: NSItemProvider){ var fetchUrl: URL? print("create from url") _ = provider.loadObject(ofClass: URL.self) { url, error in print("nested handler") <<------- never be called if let error = error { print("unwrapUrl failed: ", error.localizedDescription) } else { fetchUrl = url print("url", fetchUrl?.description) } if let url = fetchUrl{ // Do some data fetch work using url } } } }
0
0
746
Dec ’22
Drag outside screen issue during drag and drop
I write some code to drag a photo from external app to a rectangle area in my app. I want to achieve an effect that the rectangle changes to semitransparent when the photo is dragged inside the rectangle without release. And then the rectangle changes back to opacity when the photo is dragged outside of the rect area. I put my app and Photos side by side where my app is on the left side. If I drag the photo in and out from right side, it works good. However, the dropExited method is never called, if I drag the photo out from the left side. The result is the rect stays in semitransparent. It seems like the drag gesture is considered to cancel automatically INSTEAD of drop exits the area because of touching the screen border. What should I do to reset the rect to opacity in this case? import SwiftUI struct ContentView: View {     @State private var dragIn = false     var body: some View {         VStack {             Rectangle()                 .foregroundColor(.gray)                 .opacity(dragIn ? 0.5 : 1)         }         .onDrop(of: [.image], delegate: DropController(dragIn: $dragIn))     } } class DropController: DropDelegate{     @Binding var dragIn: Bool     init(dragIn: Binding<Bool>) {         _dragIn = dragIn     }     func dropExited(info: DropInfo) {         dragIn = false         print("out")     }     func dropUpdated(info: DropInfo) -> DropProposal? {         dragIn = true         print("dragging")         return nil     }     func performDrop(info: DropInfo) -> Bool {         true     } }
0
0
959
Dec ’22
pencil kit issue--drawing can't stay on canvas
I want to use pencil kit in my swiftui app. I encapsulate PKCanvasView in UIViewRepresentable. I can draw on canvas with only one stroke. However, the image will disappear immediately when I release the finger. Am I missing anything? import SwiftUI import PencilKit struct ContentView: View { var body: some View { CanvasView() } } struct CanvasView: UIViewRepresentable { func makeUIView(context: Context) -> PKCanvasView { let canvas = PKCanvasView() canvas.tool = PKInkingTool(.pen, color: .green, width: 10) canvas.drawingPolicy = .anyInput return canvas } func updateUIView(_ uiView: PKCanvasView, context: Context) { } }
1
1
1.2k
Dec ’22
cpu runs at full speed when placing offset after gesture
I want to drag the image along y-axis. It seems quite simple. However I put the "offset" following the "gesture" by mistake. Then cpu runs at full speed and UI becomes irresponsive. Everything is ok by reordering "offset" before "gesture". But I can't figure it out. "DragEffectViewModifier-ondrag" displays on the console repeatedly. What leads to the "cycle behavior"? It makes cpu to run at full speed and UI to become irresponsive when "offset" placed below "gesture" struct ContentView: View { @State private var currentPosition: CGSize = .zero @State private var newPosition: CGSize = .zero var body: some View { Image(systemName: "globe") .imageScale(.large) .foregroundColor(.accentColor) // .offset(x: currentPosition.width, y: currentPosition.height) .gesture(DragGesture() .onChanged { value in print("DragEffectViewModifier-ondrag") currentPosition = CGSize(width: 0, height: value.translation.height + newPosition.height) } .onEnded { value in newPosition = currentPosition }) .offset(x: currentPosition.width, y: currentPosition.height) } }
0
0
684
Feb ’23
matchedGeometryEffect works incorrectly
I am practising matchedGeometryEffect. struct ContentView: View { @Namespace var tem @State var isTemp: Bool = true var body: some View{ ZStack{ if isTemp{ Circle() .matchedGeometryEffect(id: "haha", in: tem, anchor: .center) .foregroundColor(.green) .frame(width: 200, height: 200) }else{ Rectangle() .matchedGeometryEffect(id: "haha", in: tem, anchor: .center) .foregroundColor(.blue).opacity(0.4) .frame(width: 400, height: 600) } Button { withAnimation(.linear(duration: 5)){ isTemp = false } } label: { Text("switch") } } } } If I set anchor to top, it works well with the tops of both shapes remaining alignment. When I set anchor to center, I expect both shapes should stay at the center point of screen and extend around. However, only the rectangle stays still as expected, while the circle descends like bottom anchor effect. What's more, if I set anchor to bottom, the circle descends more quickly. Is it a normal behavior? Do I miss anything?
0
0
687
Mar ’23
text editor & text field layout issue in list
I need use a built-in text editor, a custom text editor and a built-in text field in the same list. I encounter a couple of problems: set font to .body: The row height of custom text editor is bigger than the other two. The custom text editor can't stay in the center of the row by vertical. The text editor can't align to text field by leading. set font to .title: In addition to problem 1 behavior, the built-in text editor can't stay in the center of the row by vertical either. How could I make the three components to have the same row height and align center in vertical and align by leading? struct ContentView: View {     @State private var text = "hello"     @State private var text2 = ""     var body: some View {         List{             Section("title"){                 CustomTextEditor(text: $text)                 TextEditor(text: $text)                     .font(.title)                 TextField("hello", text: $text2)                     .font(.title)             }         }     } } struct CustomTextEditor: UIViewRepresentable{     @Binding var text: String          func makeUIView(context: Context) -> UITextView {         let textView = UITextView()         textView.isScrollEnabled = false         textView.font = UIFont.preferredFont(forTextStyle: .title1)         textView.text = text         return textView     }     func updateUIView(_ uiView: UITextView, context: Context) {              } }
0
0
666
Mar ’23
how to make custom font to align in center of the frame
I need to use custom font in my app. While the built-in standard font will align in center automatically, the custom font seems to align "top"(seeing red border part) instead of "center"(blue border part). Is there any approach to fix it? struct ContentView: View { var body: some View { VStack(spacing: 20){ Text("SEPTEMBER") .font(.largeTitle) .border(.blue) Text("SEPTEMBER") .font(Font.custom("Arial Hebrew", size: 20)) .border(.red) } } }
2
0
1.3k
Mar ’23
legal font used in iOS app
[https://developer.apple.com/fonts/system-fonts/) shows dozens of system fonts. And it says "Apple platforms come with many preinstalled fonts that can be used by your app’s user interface. " on the top of the web page. Does it mean I can use any of them in my iOS app legally? Does it involve font license issue? What else should I pay attention to use system fonts legally?
1
0
872
Mar ’23
safe area includes notch?
I'm creating a launch screen by storyboard for a swiftui app. When I rotate the iPhone 14 to landscape, the safe area has insets on vertical, without any inset on horizontal(shown as the image below). IMO, the safe area should have inset on leading, trailing and bottom edge in this case. Do I miss anything?
2
0
1.5k
Mar ’23
nested loadObject call issue
I write some code to show an image dragged from other apps(such as web browser, photos, etc). I make a delegate to perform the drop. If DropInfo has an image item, I will try to retrieve the data as uiimage by NSItemProvider.loadObject first. If errors occur during loading, I will ask DropInfo again whether it has a url item. If the answer is YES, I will try to retrieve URL by NSItemProvider.loadObject. It means the second loadObject will be nested in the first one. When running the simulator, I find the second loadObject completion handler is never called which is supposed to be called when I try to retrieve URL. Do I miss anything? import SwiftUI import Combine struct ContentView: View{ @State private var img: UIImage? var body: some View{ Image(uiImage: img != nil ? img! : UIImage(systemName: "photo")!) .resizable() .scaledToFit() .onDrop(of: [.image], delegate: ImageDropController(img: $img)) } } class ImageDropController: DropDelegate{ @Binding var img: UIImage? init(img: Binding<UIImage?>) { _img = img } func performDrop(info: DropInfo) -> Bool { if getImgFromImage(info: info){ return true }else{ return false } } func getImgFromImage(info: DropInfo) -> Bool{ guard info.hasItemsConforming(to: [.image]) else { return getImgFromUrl(info: info) } createImgFromImage(from: info.itemProviders(for: [.image]).first!,info: info) return true } func createImgFromImage(from provider: NSItemProvider, info: DropInfo){ provider.loadObject(ofClass: UIImage.self) { image, error in var unwrappedImage: UIImage? if let error = error { print("unwrapImage failed: ", error.localizedDescription) _ = self.getImgFromUrl(info: info) } else { unwrappedImage = image as? UIImage } if let image = unwrappedImage{ DispatchQueue.main.async { self.img = image } } } } func getImgFromUrl(info: DropInfo) -> Bool{ guard info.hasItemsConforming(to: [.url]) else { return false } createImgFromUrl(from: info.itemProviders(for: [.url]).first!) return true } private func createImgFromUrl(from provider: NSItemProvider){ var fetchUrl: URL? print("create from url") _ = provider.loadObject(ofClass: URL.self) { url, error in print("nested handler") <<------- never be called if let error = error { print("unwrapUrl failed: ", error.localizedDescription) } else { fetchUrl = url print("url", fetchUrl?.description) } if let url = fetchUrl{ // Do some data fetch work using url } } } }
Replies
0
Boosts
0
Views
746
Activity
Dec ’22
Drag outside screen issue during drag and drop
I write some code to drag a photo from external app to a rectangle area in my app. I want to achieve an effect that the rectangle changes to semitransparent when the photo is dragged inside the rectangle without release. And then the rectangle changes back to opacity when the photo is dragged outside of the rect area. I put my app and Photos side by side where my app is on the left side. If I drag the photo in and out from right side, it works good. However, the dropExited method is never called, if I drag the photo out from the left side. The result is the rect stays in semitransparent. It seems like the drag gesture is considered to cancel automatically INSTEAD of drop exits the area because of touching the screen border. What should I do to reset the rect to opacity in this case? import SwiftUI struct ContentView: View {     @State private var dragIn = false     var body: some View {         VStack {             Rectangle()                 .foregroundColor(.gray)                 .opacity(dragIn ? 0.5 : 1)         }         .onDrop(of: [.image], delegate: DropController(dragIn: $dragIn))     } } class DropController: DropDelegate{     @Binding var dragIn: Bool     init(dragIn: Binding<Bool>) {         _dragIn = dragIn     }     func dropExited(info: DropInfo) {         dragIn = false         print("out")     }     func dropUpdated(info: DropInfo) -> DropProposal? {         dragIn = true         print("dragging")         return nil     }     func performDrop(info: DropInfo) -> Bool {         true     } }
Replies
0
Boosts
0
Views
959
Activity
Dec ’22
pencil kit issue--drawing can't stay on canvas
I want to use pencil kit in my swiftui app. I encapsulate PKCanvasView in UIViewRepresentable. I can draw on canvas with only one stroke. However, the image will disappear immediately when I release the finger. Am I missing anything? import SwiftUI import PencilKit struct ContentView: View { var body: some View { CanvasView() } } struct CanvasView: UIViewRepresentable { func makeUIView(context: Context) -> PKCanvasView { let canvas = PKCanvasView() canvas.tool = PKInkingTool(.pen, color: .green, width: 10) canvas.drawingPolicy = .anyInput return canvas } func updateUIView(_ uiView: PKCanvasView, context: Context) { } }
Replies
1
Boosts
1
Views
1.2k
Activity
Dec ’22
cpu runs at full speed when placing offset after gesture
I want to drag the image along y-axis. It seems quite simple. However I put the "offset" following the "gesture" by mistake. Then cpu runs at full speed and UI becomes irresponsive. Everything is ok by reordering "offset" before "gesture". But I can't figure it out. "DragEffectViewModifier-ondrag" displays on the console repeatedly. What leads to the "cycle behavior"? It makes cpu to run at full speed and UI to become irresponsive when "offset" placed below "gesture" struct ContentView: View { @State private var currentPosition: CGSize = .zero @State private var newPosition: CGSize = .zero var body: some View { Image(systemName: "globe") .imageScale(.large) .foregroundColor(.accentColor) // .offset(x: currentPosition.width, y: currentPosition.height) .gesture(DragGesture() .onChanged { value in print("DragEffectViewModifier-ondrag") currentPosition = CGSize(width: 0, height: value.translation.height + newPosition.height) } .onEnded { value in newPosition = currentPosition }) .offset(x: currentPosition.width, y: currentPosition.height) } }
Replies
0
Boosts
0
Views
684
Activity
Feb ’23
how to detect major color in an image in iOS
I have a swiftui view that displays a picture which users select from Photo app and shows some texts below the picture. I want to dynamically tune the background color of the view depending on the picture major color. Say, if users select a grass or forest image, the background auto changes to green. Is there any best practice?
Replies
2
Boosts
0
Views
4.7k
Activity
Feb ’23
matchedGeometryEffect works incorrectly
I am practising matchedGeometryEffect. struct ContentView: View { @Namespace var tem @State var isTemp: Bool = true var body: some View{ ZStack{ if isTemp{ Circle() .matchedGeometryEffect(id: "haha", in: tem, anchor: .center) .foregroundColor(.green) .frame(width: 200, height: 200) }else{ Rectangle() .matchedGeometryEffect(id: "haha", in: tem, anchor: .center) .foregroundColor(.blue).opacity(0.4) .frame(width: 400, height: 600) } Button { withAnimation(.linear(duration: 5)){ isTemp = false } } label: { Text("switch") } } } } If I set anchor to top, it works well with the tops of both shapes remaining alignment. When I set anchor to center, I expect both shapes should stay at the center point of screen and extend around. However, only the rectangle stays still as expected, while the circle descends like bottom anchor effect. What's more, if I set anchor to bottom, the circle descends more quickly. Is it a normal behavior? Do I miss anything?
Replies
0
Boosts
0
Views
687
Activity
Mar ’23
text editor & text field layout issue in list
I need use a built-in text editor, a custom text editor and a built-in text field in the same list. I encounter a couple of problems: set font to .body: The row height of custom text editor is bigger than the other two. The custom text editor can't stay in the center of the row by vertical. The text editor can't align to text field by leading. set font to .title: In addition to problem 1 behavior, the built-in text editor can't stay in the center of the row by vertical either. How could I make the three components to have the same row height and align center in vertical and align by leading? struct ContentView: View {     @State private var text = "hello"     @State private var text2 = ""     var body: some View {         List{             Section("title"){                 CustomTextEditor(text: $text)                 TextEditor(text: $text)                     .font(.title)                 TextField("hello", text: $text2)                     .font(.title)             }         }     } } struct CustomTextEditor: UIViewRepresentable{     @Binding var text: String          func makeUIView(context: Context) -> UITextView {         let textView = UITextView()         textView.isScrollEnabled = false         textView.font = UIFont.preferredFont(forTextStyle: .title1)         textView.text = text         return textView     }     func updateUIView(_ uiView: UITextView, context: Context) {              } }
Replies
0
Boosts
0
Views
666
Activity
Mar ’23
how to make custom font to align in center of the frame
I need to use custom font in my app. While the built-in standard font will align in center automatically, the custom font seems to align "top"(seeing red border part) instead of "center"(blue border part). Is there any approach to fix it? struct ContentView: View { var body: some View { VStack(spacing: 20){ Text("SEPTEMBER") .font(.largeTitle) .border(.blue) Text("SEPTEMBER") .font(Font.custom("Arial Hebrew", size: 20)) .border(.red) } } }
Replies
2
Boosts
0
Views
1.3k
Activity
Mar ’23
legal font used in iOS app
[https://developer.apple.com/fonts/system-fonts/) shows dozens of system fonts. And it says "Apple platforms come with many preinstalled fonts that can be used by your app’s user interface. " on the top of the web page. Does it mean I can use any of them in my iOS app legally? Does it involve font license issue? What else should I pay attention to use system fonts legally?
Replies
1
Boosts
0
Views
872
Activity
Mar ’23
safe area includes notch?
I'm creating a launch screen by storyboard for a swiftui app. When I rotate the iPhone 14 to landscape, the safe area has insets on vertical, without any inset on horizontal(shown as the image below). IMO, the safe area should have inset on leading, trailing and bottom edge in this case. Do I miss anything?
Replies
2
Boosts
0
Views
1.5k
Activity
Mar ’23
how to display loop times in playground
I want to get the loop times info in playground. But it seems to only display the latest result for a loop on the right side. Do I miss anything? Xcode 15.0.1 import Cocoa for i in 1...5{ print("\(i)") } I have a screenshot:
Replies
2
Boosts
0
Views
1.1k
Activity
Feb ’24