gpt4 book ai didi

ios - 在其 bezierpath 中将图像添加到 CAShapeLayer

转载 作者:行者123 更新时间:2023-11-28 15:08:04 25 4
gpt4 key购买 nike

我正在努力实现 https://github.com/joshdhenry/SpinWheelControl我的应用程序中的库,对于每个轮子切片,我试图将自定义图像添加到它的 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)
}

模拟器输出:

Output in simulator

为什么我无法将图像添加到形状层??

需要输出

needed output

提前致谢

最佳答案

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
}

关于ios - 在其 bezierpath 中将图像添加到 CAShapeLayer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48144196/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com