启动图第二篇文章

2016-11-18  本文已影响41人  君赏

启动图名字生成第二篇

我们写一下配置启动图名字的页面保存按钮的逻辑

@IBAction func saveAction(_ sender: Any) {
    for index in 0..<promptNames.count {
        let view =  tableView.view(atColumn: 0, row: index, makeIfNecessary: false) // 依次获取表
        //格上的自定义cell
        guard let cell:LaunchNameCellView = view as? LaunchNameCellView else {
            continue
        }
        guard !cell.launchNameTextFiled.stringValue.isEmpty else {
            continue
        }
        promptValues[index] = cell.launchNameTextFiled.stringValue
    }

}

我们在之前的OFileManger类里面写一个保存我们启动图名字的方法。

/// 保存自定配置的启动图的名称
///
/// - Parameter list: 启动图名称的列表
func saveLaunchNames(list:[String])  {
    let userDefaults = UserDefaults.standard
    userDefaults.set(list, forKey: SLaunchNameKey)
    userDefaults.synchronize()
}

/// 读取保存的启动图的名称列表
///
/// - Returns: 字符串数组 可能读取不出来
func readLaunchNames() -> [String]? {
    let userDefaults = UserDefaults.standard
    return userDefaults.object(forKey: SLaunchNameKey) as? [String]
}

我们在点击保存按钮之后让执行保存的方法还有消失试图

SFileManger.saveLaunchNames(list: promptValues)
self.dismiss(nil)

我们在NSViewController下面的方法写逻辑,让弹出配置启动图试图时候把我们本地的配置传递过去

override func prepare(for segue: NSStoryboardSegue, sender: Any?) {
    let any = segue.destinationController
    guard let controller:LaunchNameViewController = any as? LaunchNameViewController else {
        return
    }
    guard let list = SFileManger.readLaunchNames() else {
        return
    }
    controller.promptValues = list
}

经过发现NSImage属性的Size不是真正的分辨率的大小,我们需要用到

open var representations: [NSImageRep] { get }

属性,里面的NSImageRep里面的

open var pixelsWide: Int

open var pixelsHigh: Int

我们写一个给NSImage写一个扩展的属性sSize

extension NSImage {
    var sSize:NSSize {
        get {
            var size = NSZeroSize
            if representations.count > 0 {
                let rep = representations[0]
                size = NSSize(width: rep.pixelsWide, height: rep.pixelsHigh)
            }
            return size
        }
    }
}

写一下保存的逻辑

class func saveLaunchImages(list:[NSImage], names:[String]) {
    let savePannel = NSOpenPanel()
    savePannel.canCreateDirectories = true
    savePannel.canChooseFiles = false
    savePannel.canChooseDirectories = true
    let index = savePannel.runModal()
    guard index == NSFileHandlingPanelOKButton else {
        return
    }
    guard let dic = savePannel.directoryURL else {
        return
    }
    for index in 0..<names.count {
        for image in list {
            let image640x960HD = index == 0 && NSEqualSizes(image.sSize, NSSize.init(width: 640, height: 960))
            let image640x960 = index == 1 && NSEqualSizes(image.sSize, NSSize.init(width: 640, height: 960))
            let image640x1136 = index == 2 && NSEqualSizes(image.sSize, NSSize.init(width: 640, height: 1136))
            let image750x1334 = index == 3 && NSEqualSizes(image.sSize, NSSize.init(width: 750, height: 1334))
            let image1242x2208 = index == 4 && NSEqualSizes(image.sSize, NSSize.init(width: 1242, height: 2208))
            if image640x960 || image640x960HD || image640x1136 || image750x1334 || image1242x2208 {
                let name = names[index]
                let fileName = "\(dic.absoluteString)\(name)"
                saveImage(image: image, name: fileName)
            }
        }
    }
}

class func saveImage(image:NSImage, name:String) {
    guard let file = URL.init(string: name) else {
        return
    }
    do {

        try image.tiffRepresentation?.write(to:file )
    } catch _ {

    }
}

后来有一个一键可以直接拖拽 这个功能我们百度一下怎么写?

方法基本就是NSDraggingSource, NSDraggingDestination, NSPasteboardItemDataProvider这些代理,你可以看看这个例子https://github.com/phaibin/DragDropDemo

注册接受文件的类型

override func awakeFromNib() {
    register(forDraggedTypes: [NSFilenamesPboardType])
}

完成拖拽的逻辑

override func draggingEntered(_ sender: NSDraggingInfo) -> NSDragOperation {
    guard let type = sender.draggingPasteboard().types else {
        return NSDragOperation.generic
    }
    if type.contains(NSStringPboardType) {
        isDrag = true
        self.isHighlighted = true
        return NSDragOperation.copy
    }
    return NSDragOperation.generic
}
override func draggingExited(_ sender: NSDraggingInfo?) {
    isDrag = false
    self.isHighlighted = true
}

override func prepareForDragOperation(_ sender: NSDraggingInfo) -> Bool {
    isDrag = false;
    self.isHighlighted = true
    return true;
}

override func performDragOperation(_ sender: NSDraggingInfo) -> Bool {

    guard let list = sender.draggingPasteboard().propertyList(forType: NSFilenamesPboardType) as? [String] else {
        return false
    }
    UserDefaults.standard.set(list, forKey: "dragListImage")
    UserDefaults.standard.synchronize()
    return true
}

class func readDragImageList() -> [String]? {
    return UserDefaults.standard.object(forKey: "dragListImage") as? [String]
}

上一篇下一篇

猜你喜欢

热点阅读