美文网首页
Mac osx 开发-Swift-类似NSSegmentCont

Mac osx 开发-Swift-类似NSSegmentCont

作者: Funcy1Day | 来源:发表于2017-04-21 12:46 被阅读106次

    逻辑:

    自定义NSView上面添加了一组自定义的NSButton,NSButton自定义包含了NSButton组件,以及添加了下部的条状NSView。

    import Cocoa

    private let kSelectedColor = NSColor.randomColor

    private let kNormalColor = NSColor.lightGray

    private let ScreenSize =  NSScreen.main()?.frame

    class SZNaviButton: NSButton {

    lazy var lineView: NSView = {

    let lineWidth: CGFloat = 3.0

    let rect = NSRect(x: 0, y: self.frame.size.height-lineWidth*2, width: self.frame.size.width, height: lineWidth)

    let lineView = NSView(frame: rect)

    lineView.layer?.backgroundColor = kNormalColor.cgColor

    return lineView

    }()

    override init(frame frameRect: NSRect) {

    super.init(frame: frameRect)

    lineView.layer?.backgroundColor = NSColor.red.cgColor

    self.addSubview(lineView)

    //wantsLayer 属性设置为 YES 是启用 layer backing 最简单的方法

    self.wantsLayer = true

    self.font = NSFont(name: "PingFang SC", size: 18)

    self.layer?.backgroundColor = NSColor.clear.cgColor

    self.isBordered = false

    //设置点击时候颜色

    (self.cell as! NSButtonCell).highlightsBy = NSCellStyleMask.contentsCellMask

    }

    required init?(coder: NSCoder) {

    fatalError("init(coder:) has not been implemented")

    }

    override func draw(_ dirtyRect: NSRect) {

    super.draw(dirtyRect)

    }

    func setSelected(flag:Bool) {

    if flag {

    lineView.layer?.backgroundColor = kSelectedColor.cgColor

    //            lineView.draw(self.bounds)

    let pstyle = NSMutableParagraphStyle()

    pstyle.alignment = .center

    self.attributedTitle = NSAttributedString(string: self.attributedTitle.string, attributes: [NSForegroundColorAttributeName: kSelectedColor,NSParagraphStyleAttributeName: pstyle])

    }else{

    lineView.layer?.backgroundColor = kNormalColor.cgColor

    //            lineView.draw(self.bounds)

    let pstyle = NSMutableParagraphStyle()

    pstyle.alignment = .center

    self.attributedTitle = NSAttributedString(string: self.attributedTitle.string, attributes: [NSForegroundColorAttributeName: kNormalColor,NSParagraphStyleAttributeName: pstyle])

    }

    }

    func setAttributeTitle(title:String,color: NSColor) {

    let pstyle = NSMutableParagraphStyle()

    pstyle.alignment = .center

    self.title = title

    self.attributedTitle = NSAttributedString(string: title, attributes: [NSForegroundColorAttributeName: color,NSParagraphStyleAttributeName: pstyle])

    }

    }

    protocol SZSegmentViewDelegate: class {

    func selectedIndex(index: Int)

    }

    class SZSegmentView: NSView {

    weak var delegate: SZSegmentViewDelegate?

    var lastClickButton: SZNaviButton!

    init(titles: NSArray, andFrame frame: NSRect) {

    super.init(frame: frame)

    let buttonWidth = self.frame.width / CGFloat(titles.count)

    for i  in 0 ..< titles.count {

    let rect = NSRect(x: CGFloat(i)*buttonWidth, y: 0, width: buttonWidth, height: frame.height)

    let button = SZNaviButton(frame: rect)

    button.setAttributeTitle(title: titles[i] as! String, color: NSColor.gray)

    if i==0 {

    button.setSelected(flag: true)

    lastClickButton = button

    }else{

    button.setSelected(flag: false)

    }

    button.viewTag = i

    button.target = self

    button.action = #selector(btnChosed(sender:))

    self.addSubview(button)

    }

    }

    required init?(coder: NSCoder) {

    fatalError("init(coder:) has not been implemented")

    }

    func btnChosed(sender: SZNaviButton) {

    if lastClickButton != sender {

    sender.setSelected(flag: true)

    lastClickButton.setSelected(flag: false)

    lastClickButton = sender

    self.delegate?.selectedIndex(index: sender.viewTag)

    }

    }

    override func draw(_ dirtyRect: NSRect) {

    super.draw(dirtyRect)

    }

    deinit {

    self.delegate = nil

    }

    }

    相关文章

      网友评论

          本文标题:Mac osx 开发-Swift-类似NSSegmentCont

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