MacOS 开发(十五) : NSViewController 的 present 方法总结

MacOS 开发(十五) : NSViewController 的 present 方法总结一、需求来源 mac控制器呈现有 transition、presentAsSheet、presentAsModalWindow、present几种实现方式。 二、使用示例 Github

一、需求来源

mac控制器呈现有 transition、presentAsSheet、presentAsModalWindow、present几种实现方式。

asModel.png

asSheet.jpg

asPopover.png

二、使用示例

class ShowViewController: NSViewController {

    var itemList: [NSButton] = []

    var isChoose = false
    
    var vcOne = BlueViewController()
    var vcTwo = GreenViewController()
    var vcThree = OrangeViewController()
    
    // MARK: -lifecycle
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do view setup here.
        let list: [String] = ["Next", "Front", "AsSheet", "AsModal", "asPopover", "Button", ]
        itemList = NSButton.createGroupView(.zero, list: list, numberOfRow: 6, padding: 8, target: self, action: #selector(handleAction(_:)), inView: view);
        
        addChild(vcOne)
        addChild(vcTwo)
                
        view.addSubview(vcOne.view)
        view.addSubview(vcTwo.view)
    }
    
    override func viewDidLayout() {
        super.viewDidLayout()
        
        let frame = CGRect(x: 0, y: 0, width: view.bounds.width, height: view.bounds.height*0.1)
        NSButton.setupConstraint(frame, items: itemList, numberOfRow: 6, padding: 8)
// print("\(#function)_\(view.bounds.size)_\(vcOne.view.bounds.size)")

        for e in children.enumerated() {
            e.element.view.frame = CGRectMake(40, frame.maxY, view.bounds.width - 80, view.bounds.height - frame.maxY)
        }
    }
    
    // MARK: -funtions
    @objc func handleAction(_ sender: NNButton) {
// DDLog("\(sender.tag)")

        switch sender.tag {
        case 0:
            if children[0].view.superview == nil {
                return
            }
            transition(from: children[0], to: children[1], options: .slideLeft, completionHandler: nil)

        case 1:
            if children[1].view.superview == nil {
                return
            }
            transition(from: children[1], to: children[0], options: .slideRight, completionHandler: nil)

        case 2:
            presentAsSheet(vcOne)
            
        case 3:
            DDLog("\(presentingViewController)\(presentedViewControllers)")
            //问题: dismiss时presentedViewControllers不会减少
            presentAsModalWindow(vcOne)
            
        case 4:
            if presentedViewControllers?.count != 0 {
                dismiss(vcThree)
                return
            }
            
            present(vcThree, asPopoverRelativeTo: view.frame, of: sender, preferredEdge: .maxY, behavior: .semitransient)
        default:
            break;
        }
    }
}

class BlueViewController: NSViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do view setup here.
        view.layer?.backgroundColor = NSColor.lightBlue.cgColor
    }
    
    override func viewWillAppear() {
        super.viewWillAppear()
        
    }
    
    override func mouseDown(with event: NSEvent) {
        if presentingViewController == nil {
            return
        }
        dismiss(self)
    }
}

Github

今天的文章MacOS 开发(十五) : NSViewController 的 present 方法总结分享到此就结束了,感谢您的阅读。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/15349.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注