Ios 按y坐标切割Bezier路径
我得到了具体的Ios 按y坐标切割Bezier路径,ios,swift,core-graphics,uibezierpath,Ios,Swift,Core Graphics,Uibezierpath,我得到了具体的UIBezierPath,例如 override func drawInContext(ctx: CGContext) { if let slider = slider { // Clip let rect = bounds.insetBy(dx: bounds.width / 10, dy: bounds.height / 2.2) let path = UIBezierPath(roundedRect:
UIBezierPath
,例如
override func drawInContext(ctx: CGContext) {
if let slider = slider {
// Clip
let rect = bounds.insetBy(dx: bounds.width / 10, dy: bounds.height / 2.2)
let path = UIBezierPath(roundedRect: rect, cornerRadius: 5)
let circleRadius : CGFloat = 10
let xCoordInset = bounds.width / 10
let circlePath = UIBezierPath(ovalInRect: CGRectMake(xCoordInset , rect.midY - circleRadius, circleRadius * 2, circleRadius * 2))
let circlePath1 = UIBezierPath(ovalInRect: CGRectMake(bounds.width - xCoordInset - circleRadius, rect.midY - circleRadius, circleRadius * 2, circleRadius * 2))
let circlePath2 = UIBezierPath(ovalInRect: CGRectMake(rect.midX - circleRadius, rect.midY - circleRadius, circleRadius * 2, circleRadius * 2))
path.appendPath(circlePath)
path.appendPath(circlePath1)
path.appendPath(circlePath2)
CGContextAddPath(ctx, path.CGPath)
CGContextSetFillColorWithColor(ctx, slider.trackTintColor.CGColor)
CGContextFillPath(ctx)
}
}
我想用灰色填充这个bezierPath的一半,用红色填充另一半。因此,我假设我需要有两个相同的图层,但其中一个图层应该按y坐标剪切,你能为这一操作提供一些可用的方法吗?在填充灰色之前,将其剪切到剪切线上方的矩形上。然后剪辑到切割线下方的矩形,并用红色填充
override func drawInContext(ctx: CGContext) {
if let slider = slider {
// Clip
let rect = bounds.insetBy(dx: bounds.width / 10, dy: bounds.height / 2.2)
let path = UIBezierPath(roundedRect: rect, cornerRadius: 5)
let circleRadius : CGFloat = 10
let xCoordInset = bounds.width / 10
let circlePath = UIBezierPath(ovalInRect: CGRectMake(xCoordInset , rect.midY - circleRadius, circleRadius * 2, circleRadius * 2))
let circlePath1 = UIBezierPath(ovalInRect: CGRectMake(bounds.width - xCoordInset - circleRadius, rect.midY - circleRadius, circleRadius * 2, circleRadius * 2))
let circlePath2 = UIBezierPath(ovalInRect: CGRectMake(rect.midX - circleRadius, rect.midY - circleRadius, circleRadius * 2, circleRadius * 2))
path.appendPath(circlePath)
path.appendPath(circlePath1)
path.appendPath(circlePath2)
let yCut = bounds.midY // or whatever
CGContextSaveGState(ctx); do {
CGContextClipToRect(ctx, CGRectMake(bounds.minX, bounds.minY, bounds.width, yCut - bounds.minY))
CGContextAddPath(ctx, path.CGPath)
CGContextSetFillColorWithColor(ctx, slider.trackTintColor.CGColor)
CGContextFillPath(ctx)
}; CGContextRestoreGState(ctx)
CGContextSaveGState(ctx); do {
CGContextClipToRect(ctx, CGRectMake(bounds.minX, yCut, bounds.width, bounds.maxY - yCut))
CGContextAddPath(ctx, path.CGPath)
CGContextSetFillColorWithColor(ctx, slider.progressTintColor.CGColor)
CGContextFillPath(ctx)
}; CGContextRestoreGState(ctx)
}
}
您可以创建两个相同的形状,并对其中一个应用遮罩