美文网首页
SwiftUI编写macOS App ---- AppIcon

SwiftUI编写macOS App ---- AppIcon

作者: 狂奔的胖蜗牛 | 来源:发表于2021-02-01 20:19 被阅读0次

我们都知道,iOS的图标是需要自己设置的,如下:


image.png

有时候,UI给我们的icon很有可能缺了某一个,或者说某一个尺寸不对,有一个办法是找UI重新给我们重新做一下尺寸,但是有可能需要等一段时间,而我们是急着马上要用的,我们可以自己生成嘛。这就是AppIcon应用的作用,只要一张图片,就能够生成所有所需的icon,再也不用求人了。

使用SwiftUI编写UI界面

以前没有写过macOS的软件,但是SwiftUI会写,于是,我用SwiftUI写了界面。

直接上UI界面全部代码。

struct ContentView: View {
    
    @State private var path = ""
    @State private var alertShow = false
    @State private var errorAlertShow = false
    @State private var choiceDevice = ""
    @State private var errorMSG = ""
    @State private var image: NSImage?
    @State private var dragOver = false
    
    var body: some View {
        VStack {
            Group {
                Text(TextTools.Tips)
                    .padding()
                Spacer()
                Image(nsImage: image ?? NSImage(named: "add")!)
                    .resizable()
                    .scaledToFit()
                    .frame(width: 250, height: 250)
                    .onTapGesture {
                        showChoiceImagePanel()
                    }
                    .onDrop(of: ["public.file-url"], isTargeted: $dragOver, perform: { providers -> Bool in
                        providers.first?.loadDataRepresentation(forTypeIdentifier: "public.file-url", completionHandler: { (data, error) in
                            if let data = data, let path = NSString(data: data, encoding: 4),
                               let url = URL(string: path as String) {
                                let image = NSImage(contentsOf: url)
                                self.image = image
                            }
                        })
                        return true
                    })
                    .alert(isPresented: $errorAlertShow) { () -> Alert in
                        Alert(title: Text(TextTools.AlertTitle),
                              message: Text(errorMSG),
                              dismissButton: .default(Text(TextTools.Sure)))
                    }
                Spacer()
            }
            HStack {
                Button(TextTools.OpenPath) {
                    openImagePath()
                }.background(Color.green.cornerRadius(5)).padding()
                TextField(TextTools.Path, text: $path)
                    .padding()
                    .disabled(true)
                Button(TextTools.Choice) {
                    showImageSavePanel()
                }.background(Color.green.cornerRadius(5)).padding()
            }
            HStack {
                Button(TextTools.iPhone) {
                    guard checkError() else {
                        return
                    }
                    choiceDevice = TextTools.iPhone
                    alertShow.toggle()
                }.background(Color.green.cornerRadius(5))
                Spacer()
                Button(TextTools.iMac) {
                    guard checkError() else {
                        return
                    }
                    choiceDevice = TextTools.iMac
                    alertShow.toggle()
                }.background(Color.green.cornerRadius(5))
                Spacer()
                Button(TextTools.iWatch) {
                    guard checkError() else {
                        return
                    }
                    choiceDevice = TextTools.iWatch
                    alertShow.toggle()
                }.background(Color.green.cornerRadius(5))
            }.padding()
            .alert(isPresented: $alertShow) { () -> Alert in
                Alert(title: Text(TextTools.AlertTitle),
                      message: Text(TextTools.AlertMSG1 + choiceDevice + TextTools.AlertMSG2),
                      primaryButton: .default(Text(TextTools.Sure),
                                              action: {
                                                if choiceDevice == TextTools.iPhone {
                                                    saveIPhoneIcon()
                                                } else if choiceDevice == TextTools.iMac {
                                                    saveMACIcon()
                                                } else if choiceDevice == TextTools.iWatch {
                                                    saveWatchIcon()
                                                }
                                              }),
                      secondaryButton: .default(Text(TextTools.Cancel)))
            }
        }.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
    }
}

是的,你没有看错,UI界面就这么些代码,最终运行结果如下:


image.png

然后把剩下的逻辑处理相关代码添加上去,最终项目完整代码:https://github.com/flywo/AppIcon

相关文章

网友评论

      本文标题:SwiftUI编写macOS App ---- AppIcon

      本文链接:https://www.haomeiwen.com/subject/ymystltx.html