Ios 在bezier圆弧内绘制子层
我试图在一个圆弧内画一系列的垂直线,但我很难做到这一点。我尝试使用Ios 在bezier圆弧内绘制子层,ios,swift,uiview,cashapelayer,Ios,Swift,Uiview,Cashapelayer,我试图在一个圆弧内画一系列的垂直线,但我很难做到这一点。我尝试使用CAShapeLayers来实现这一点,最终结果是这样的 我知道如何使用CAShapeLayer绘制曲线和线段,但我似乎不知道如何在CAShapeLayer 我最初的方法是将CAShapeLayer子类化,并在子类中尝试绘制垂直线。然而,我没有得到想要的结果。下面是我的代码,用于将线添加到贝塞尔点并尝试添加子层 class CustomLayer : CAShapeLayer { override init() {
CAShapeLayers
来实现这一点,最终结果是这样的
我知道如何使用CAShapeLayer绘制曲线和线段,但我似乎不知道如何在CAShapeLayer
我最初的方法是将CAShapeLayer子类化,并在子类中尝试绘制垂直线。然而,我没有得到想要的结果。下面是我的代码,用于将线添加到贝塞尔点并尝试添加子层
class CustomLayer : CAShapeLayer {
override init() {
super.init()
}
func drawDividerLayer(){
print("Init has been called in custom layer")
print("The bounds of the custom layer is: \(bounds)")
print("The frame of the custom layer is: \(frame)")
let bezierPath = UIBezierPath()
let dividerShapeLayer = CAShapeLayer()
dividerShapeLayer.strokeColor = UIColor.redColor().CGColor
dividerShapeLayer.lineWidth = 1
let startPoint = CGPointMake(5, 0)
let endPoint = CGPointMake(5, 8)
let convertedStart = convertPoint(startPoint, toLayer: dividerShapeLayer)
let convertedEndPoint = convertPoint(endPoint, toLayer: dividerShapeLayer)
bezierPath.moveToPoint(convertedStart)
bezierPath.addLineToPoint(convertedEndPoint)
dividerShapeLayer.path = bezierPath.CGPath
addSublayer(dividerShapeLayer)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
class DrawView : UIView {
var customDrawLayer : CAShapeLayer!
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
//drawLayers()
}
override func drawRect(rect: CGRect) {
super.drawRect(rect)
}
func drawLayers() {
let bezierPath = UIBezierPath()
let startPoint = CGPointMake(5, 35)
let endPoint = CGPointMake(100, 35)
bezierPath.moveToPoint(startPoint)
bezierPath.addLineToPoint(endPoint)
let customLayer = CustomLayer()
customLayer.frame = CGPathGetBoundingBox(bezierPath.CGPath)
customLayer.drawDividerLayer()
customLayer.strokeColor = UIColor.blackColor().CGColor
customLayer.opacity = 0.5
customLayer.lineWidth = 8
customLayer.fillColor = UIColor.clearColor().CGColor
layer.addSublayer(customLayer)
customLayer.path = bezierPath.CGPath
}
但是,此代码生成此图像:
显然,我有一个坐标空间问题/边界/框架问题,但我不太确定。我希望这样做的方式是在CustomLayer类中从超层的顶部绘制到超层的底部。但不仅如此,这必须使用bezier路径
addArcWithCenter:
方法来实现,我还没有使用该方法,因为我正在尝试首先解决这个问题。任何帮助都将不胜感激 绘制由直线组成的圆弧的最简单方法是使用lineDashPattern
:
let path = UIBezierPath(arcCenter: center, radius: radius, startAngle: 0, endAngle: CGFloat(M_PI), clockwise: false)
let arc = CAShapeLayer()
arc.path = path.CGPath
arc.lineWidth = 50
arc.lineDashPattern = [4,15]
arc.strokeColor = UIColor.lightGrayColor().CGColor
arc.fillColor = UIColor.clearColor().CGColor
view.layer.addSublayer(arc)
这是一条蓝色的弧线,在上面的虚线弧线下面。显然,我为了可见性而放大了它,但它说明了这个想法
完美!这正是我需要的。非常感谢。