我是新手,不知道如何解决这个问题。
我想设计一个自定义视图,我希望它具有一个圆形的按钮。(附加屏幕截图)
我能够通过使用图层并使用以下命令添加图层来做到这一点:
// 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
}
这工作得很好,但是我想使用相同的形状,但是我不想使用图层,而是要定义Buttons而不是视图中的图层。如果我尝试直接添加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
。 。