需要利用三方库:YMTreeMap
效果图如下:
代码如下:
//MARK: 不规则试图布局
func reloadDetailView(list:[FileInformation]) -> Void {
let new_list = list.sorted(by: {$0.size > $1.size})
if detailbgView.subviews.count > 0 {
for obj in detailbgView.subviews {
obj.removeFromSuperview()
}
}
var sizeArr = [Double]()
var arrnum = 0
if new_list.count > 0 {
for model in new_list {
if model.size != 0 && arrnum < 10 {
arrnum += 1
sizeArr.append(Double(model.size))
}
}
}
if sizeArr.count == 0 {
//空目录
detailbgView.isHidden = true
NotFileBgView.isHidden = false
return
}
detailbgView.isHidden = false
NotFileBgView.isHidden = true
//12个数做log运算
sizeArr = changeNumToLog(list: sizeArr)
let treeMapRect = NSRect.init(x: 0, y: 0, width: detailbgView.frame.width , height: detailbgView.frame.height)
let values = sizeArr
let tm = YMTreeMap.init(withValues: values)
let treeMapRects = tm.tessellate(inRect: treeMapRect)
//随机获取item背景渐变色
let aGradient1 = NSGradient(colors: [NSColor(hex: "A18CD1"),NSColor(hex: "FBC2EB")])
let aGradient2 = NSGradient(colors: [NSColor(hex: "16D9E3"),NSColor(hex: "30C7EC"),NSColor(hex: "46AEF7")])
let aGradient3 = NSGradient(colors: [NSColor(hex: "FF9A9E"),NSColor(hex: "FAD0C4"),NSColor(hex: "FAD0C4")])
let aGradient4 = NSGradient(colors: [NSColor(hex: "C471F5"),NSColor(hex: "FA71CD")])
let aGradient5 = NSGradient(colors: [NSColor(hex: "FCCB90"),NSColor(hex: "D57EEB")])
let aGradient6 = NSGradient(colors: [NSColor(hex: "37ECBA"),NSColor(hex: "72AFD3")])
let aGradient7 = NSGradient(colors: [NSColor(hex: "F6D365"),NSColor(hex: "FDA085")])
let aGradient8 = NSGradient(colors: [NSColor(hex: "A1C4FD"),NSColor(hex: "C2E9FB")])
let aGradient9 = NSGradient(colors: [NSColor(hex: "E0C3FC"),NSColor(hex: "8EC5FC")])
let aGradient10 = NSGradient(colors: [NSColor(hex: "5EE7DF"),NSColor(hex: "B490CA")])
let aGradient11 = NSGradient(colors: [NSColor(hex: "9890E3"),NSColor(hex: "B1F4CF")])
let aGradient12 = NSGradient(colors: [NSColor(hex: "37ECBA"),NSColor(hex: "72AFD3")])
let aGradient13 = NSGradient(colors: [NSColor(hex: "EBBBA7"),NSColor(hex: "CFC7F8")])
let colors = [aGradient1,aGradient2,aGradient3,aGradient4,aGradient5,aGradient6,aGradient7,aGradient8,aGradient9,aGradient10,aGradient11,aGradient12,aGradient13]
var nums = 0
for rectVal in treeMapRects {
let rectVals = NSValue(rect: rectVal)
let spaceViewRect1 = CGRectMake(rectVals.rectValue.origin.x+2, rectVals.rectValue.origin.y+2, rectVals.rectValue.size.width, rectVals.rectValue.size.height )
let view = PMMDiskAnaysisSpaceView.init(frame: spaceViewRect1)
let num = arc4random() % 13
let configKey = colors[Int(num)]
view.tag = nums
view.gradient = configKey
view.model = new_list[nums]
view.delegate = self
view.moveDelegate = self
detailbgView.addSubview(view)
nums += 1
}
}
//MARK: log运算
func changeNumToLog(list:[Double]) -> [Double] {
var sizeArr = [Double]()
for obj in list {
let num = log(obj) * log(obj) * log(obj) * log(obj) * log(obj) * log(obj) * log(obj) * log(obj) * log(obj) * log(obj) * log(obj) * log(obj)
if sizeArr.count > 0 {
guard let firstNum = sizeArr.first else { return [] }
let ratio = Double(num)/(firstNum * 1.0)
if ratio > 0.02 {
sizeArr.append(Double(num))
}
}else{
sizeArr.append(Double(num))
}
}
return sizeArr
}