邏輯:
自定義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 屬性設(shè)置為 YES 是啟用 layer backing 最簡單的方法
self.wantsLayer = true
self.font = NSFont(name: "PingFang SC", size: 18)
self.layer?.backgroundColor = NSColor.clear.cgColor
self.isBordered = false
//設(shè)置點(diǎn)擊時(shí)候顏色
(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
}
}