Я новичок в Swift и не знаю, как с этим работать.
Я хотел создать собственный вид, в котором я хочу иметь кнопки в форме круга. (Скриншот прилагается)
Мне удалось сделать это, используя слои и добавляя слои, используя следующее:
// radius is the half the frame's width or height (whichever is smallest)
let radius = min(frame.size.width, frame.size.height) * 0.5
// center of the view
let viewCenter = CGPoint(x: bounds.size.width * 0.5, y: bounds.size.height * 0.5)
// enumerate the total value of the segments by using reduce to sum them
var valueCount = outerSegments.reduce(0, {$0 + $1.value})
var startAngle : CGFloat = 0
var i = 0
// var data: [String] = ["","","","","","","","","","","",""]
for segment in outerSegments { // loop through the values array
let endAngle = startAngle + 2 * .pi * (segment.value / valueCount)
let shape = CAShapeLayer()
let path: UIBezierPath = UIBezierPath(arcCenter: viewCenter, radius: radius, startAngle: startAngle, endAngle: endAngle, clockwise: true)
path.addLine(to: viewCenter)
path.close()
shape.path = path.cgPath
shape.fillColor = segment.color.cgColor
shape.strokeColor = UIColor.black.cgColor
shape.borderWidth = 1.0
shape.borderColor = UIColor.black.cgColor
shape.name = "\(i)"
datamap[shape.name ?? ""] = data[i]
//addText(data[i], to: shape)
layer.addSublayer(shape)
startAngle = endAngle
i += 1
}
valueCount = innerSegments.reduce(0, {$0 + $1.value})
startAngle = 0
i = 100
for segment in innerSegments {
let endAngle = startAngle + 2 * .pi * (segment.value / valueCount)
let shape = CAShapeLayer()
let path: UIBezierPath = UIBezierPath(arcCenter: viewCenter, radius: 2*radius/3, startAngle: startAngle, endAngle: endAngle, clockwise: true)
path.addLine(to: viewCenter)
path.close()
shape.path = path.cgPath
shape.strokeColor = UIColor.black.cgColor
shape.fillColor = segment.color.cgColor
shape.borderWidth = 1.0
shape.borderColor = UIColor.black.cgColor
shape.name = "\(i)"
//datamap[shape.name ?? ""] = data[i-100]
//addText(data[i-100], to: shape)
layer.addSublayer(shape)
startAngle = endAngle
i += 1
}
valueCount = innerMostSegments.reduce(0, {$0 + $1.value})
startAngle = 0
i = 1000
for segment in innerMostSegments {
let endAngle = startAngle + 2 * .pi * (segment.value / valueCount)
let shape = CAShapeLayer()
let path: UIBezierPath = UIBezierPath(arcCenter: viewCenter, radius: radius/3, startAngle: startAngle, endAngle: endAngle, clockwise: true)
path.addLine(to: viewCenter)
path.close()
shape.path = path.cgPath
shape.fillColor = segment.color.cgColor
shape.strokeColor = UIColor.black.cgColor
shape.borderWidth = 1.0
shape.borderColor = UIColor.black.cgColor
shape.name = "\(i)"
datamap[shape.name ?? ""] = data[i-1000]
//addText(data[i-1000], to: shape)
layer.addSublayer(shape)
startAngle = endAngle
i += 1
}
Это работает отлично, но я хочу работать с той же формой, но вместо использования слоя я хочу определить кнопки вместо слоев в представлении. Если я попытаюсь добавить UIButton напрямую, экран больше не будет доступен.
Может кто посоветует с таким же.
Спасибо
РЕДАКТИРОВАТЬ - Я попытался добавить кнопку, используя следующую стратегию:
for segment in innerSegments {
let endAngle = startAngle + 2 * .pi * (segment.value / valueCount)
let shape = CAShapeLayer()
let button = UIButton(frame: accessibilityFrame)
let path: UIBezierPath = UIBezierPath(arcCenter: viewCenter, radius: radius, startAngle: startAngle, endAngle: endAngle, clockwise: true)
path.addLine(to: viewCenter)
path.close()
shape.path = path.cgPath
shape.fillColor = segment.color.cgColor
shape.strokeColor = UIColor.black.cgColor
shape.borderWidth = 1.0
shape.borderColor = UIColor.black.cgColor
shape.name = "\(i)"
datamap[shape.name ?? ""] = data[i]
//addText(data[i], to: shape)
button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
button.layer.addSublayer(shape)
layer.addSublayer(button.layer)
startAngle = endAngle
i += 1
. . .