我正在努力实施 我的应用程序中的David M. Gay's pow5mult库,我试图将自定义图像添加到shapelayer中,或者在切片内部以任何其他方式添加到其边界和路径,我无法这样做。我尝试将图像添加到新shapelayer的内容中并将其添加到我的饼图中,但它也不起作用。
for wedgeNumber in 0..<numberOfWedges {
let wedge: SpinWheelWedge = source.wedgeForSliceAtIndex(index: wedgeNumber)
//Wedge shape
wedge.shape.configureWedgeShape(index: wedgeNumber, radius: radius, position: spinWheelCenter, degreesPerWedge: degreesPerWedge)
wedge.layer.addSublayer(wedge.shape)
let layer = CAShapeLayer()
layer.contents = UIImage(named: "sample.jpg")?.cgImage
layer.bounds = wedge.shape.bounds
layer.path = wedge.shape.path
wedge.shape.addSublayer(layer)
spinWheelView.addSubview(wedge)
}
模拟器中的输出:
为什么我无法将图像添加到形状图层?
需要的输出
提前致谢
答案 0 :(得分:1)
class CustomImageView: UIImageView{
var bezierPath = UIBezierPath()
override func setNeedsLayout() {
bezierPath.reversing()
let shapeLayer = CAShapeLayer()
shapeLayer.frame = self.bounds
shapeLayer.path = bezierPath.cgPath
self.layer.mask = shapeLayer
self.layer.masksToBounds = true
}
}
您修改后的代码
for wedgeNumber in 0..<numberOfWedges {
let wedge: SpinWheelWedge = source.wedgeForSliceAtIndex(index: wedgeNumber)
//Wedge shape
wedge.shape.configureWedgeShape(index: wedgeNumber, radius: radius, position: spinWheelCenter, degreesPerWedge: degreesPerWedge)
wedge.layer.addSublayer(wedge.shape)
let imageLayer = CustomImageView()
imageLayer.image = UIImage(named: "sample.jpg")
imageLayer.bezierPath = wedge.shape.path
imageLayer.setNeedsLayout()
// Here you will get an image view with specified path
you can subview this imageLayer to the required view
}