我设计了这段代码来生成Bezier路径,该路径用作CAShapeLayer掩盖UIView的路径(视图的高度和宽度是可变的)
此代码生成一个边缘锐利的三角形,但我想让它成为圆角!我花了2个小时尝试使用addArcWithCenter...
,lineCapStyle
和lineJoinStyle
等,但似乎没有任何对我有用。
UIBezierPath *bezierPath = [UIBezierPath bezierPath];
CGPoint center = CGPointMake(rect.size.width / 2, 0);
CGPoint bottomLeft = CGPointMake(10, rect.size.height - 0);
CGPoint bottomRight = CGPointMake(rect.size.width - 0, rect.size.height - 0);
[bezierPath moveToPoint:center];
[bezierPath addLineToPoint:bottomLeft];
[bezierPath addLineToPoint:bottomRight];
[bezierPath closePath];
所以我的问题是,如何绕过UIBezierPath中三角形的所有边缘(我需要子层,多路径等)吗?
N.B我没有绘制这个BezierPath,因此CGContext...
中的所有drawRect
函数都无法帮助我:(
谢谢!
答案 0 :(得分:104)
FWIW:此答案通过解释CGPathAddArcToPoint(...)
为您做的事情来实现其教育目的。我强烈建议您仔细阅读,因为它将帮助您理解和欣赏CGPath API。然后你应该继续使用它,如an0's answer中所示,而不是在您的应用程序中使用边缘时使用此代码。如果您想要使用此类代码,请仅使用此代码作为参考。
因为我觉得这样的问题很有趣,所以我不得不回答:)
这是一个很长的答案。没有简短的版本:D
注意:为了我自己的简单,我的解决方案是对用于形成三角形的点做出一些假设,例如:
如果你愿意,你可以使用相同的技术来对任何多边形进行舍入,只要它是严格凸的(即不是尖头星)。我不会解释如何做,但它遵循相同的原则。
这一切都以一个三角形开始,你想要绕着一些半径的角落, r :
圆角三角形应该包含在尖三角形中,所以第一步是找到尽可能靠近角落的位置,你可以在这里找到一个半径为r的圆圈。
这样做的一个简单方法是在三角形中创建3条与3条边平行的新线,并将每条距离 r 向内移动,与原边的正面相交。
为此,您需要计算每条线的斜率/角度以及应用于两个新点的偏移量:
CGFloat angle = atan2f(end.y - start.y,
end.x - start.x);
CGVector offset = CGVectorMake(-sinf(angle)*radius,
cosf(angle)*radius);
注意:为了清楚起见,我使用的是CGVector类型(在iOS 7中可用),但您也可以使用点或大小来处理以前的操作系统版本。
然后将偏移量添加到每行的起点和终点:
CGPoint offsetStart = CGPointMake(start.x + offset.dx,
start.y + offset.dy);
CGPoint offsetEnd = CGPointMake(end.x + offset.dx,
end.y + offset.dy);
当你这样做时,你会看到三条线在三个地方相互交叉:
每个交叉点恰好是 r 距离两个边的距离(假设三角形足够大,如上所述)。
您可以将两条线的交点计算为:
// (x1⋅y2-y1⋅x2)(x3-x4) - (x1-x2)(x3⋅y4-y3⋅x4)
// px = –––––––––––––––––––––––––––––––––––––––––––
// (x1-x2)(y3-y4) - (y1-y2)(x3-x4)
// (x1⋅y2-y1⋅x2)(y3-y4) - (y1-y2)(x3⋅y4-y3⋅x4)
// py = –––––––––––––––––––––––––––––––––––––––––––
// (x1-x2)(y3-y4) - (y1-y2)(x3-x4)
CGFloat intersectionX = ((x1*y2-y1*x2)*(x3-x4) - (x1-x2)*(x3*y4-y3*x4)) / ((x1-x2)*(y3-y4) - (y1-y2)*(x3-x4));
CGFloat intersectionY = ((x1*y2-y1*x2)*(y3-y4) - (y1-y2)*(x3*y4-y3*x4)) / ((x1-x2)*(y3-y4) - (y1-y2)*(x3-x4));
CGPoint intersection = CGPointMake(intersectionX, intersectionY);
其中(x1,y1)至(x2,y2)是第一行,(x3,y3)至(x4,y4)是第二行。
如果然后在每个交点上放置一个半径为 r 的圆,则可以看到它确实是圆角三角形(忽略三角形和圆形的不同线宽) :
现在要创建一个圆角三角形,您要创建一条路径,该路径在原始三角形与交叉点正交的点上从一条线变为一条弧,再变为一条线(等)。这也是圆与原始三角形相切的点。
知道三角形中所有3个边的斜率,圆角半径和圆的中心(交点),每个圆角的起始和终止角度是该边的斜率--90度。为了将这些东西组合在一起,我在我的代码中创建了一个结构,但如果你不想这样做,你就不必这样做:
typedef struct {
CGPoint centerPoint;
CGFloat startAngle;
CGFloat endAngle;
} CornerPoint;
为了减少代码重复,我为自己创建了一个方法,计算一个点的交点和角度给定从一个点到另一个点的线到最终点(它没有关闭所以它不是三角形):
代码如下(它实际上只是我上面显示的代码,放在一起):
- (CornerPoint)roundedCornerWithLinesFrom:(CGPoint)from
via:(CGPoint)via
to:(CGPoint)to
withRadius:(CGFloat)radius
{
CGFloat fromAngle = atan2f(via.y - from.y,
via.x - from.x);
CGFloat toAngle = atan2f(to.y - via.y,
to.x - via.x);
CGVector fromOffset = CGVectorMake(-sinf(fromAngle)*radius,
cosf(fromAngle)*radius);
CGVector toOffset = CGVectorMake(-sinf(toAngle)*radius,
cosf(toAngle)*radius);
CGFloat x1 = from.x +fromOffset.dx;
CGFloat y1 = from.y +fromOffset.dy;
CGFloat x2 = via.x +fromOffset.dx;
CGFloat y2 = via.y +fromOffset.dy;
CGFloat x3 = via.x +toOffset.dx;
CGFloat y3 = via.y +toOffset.dy;
CGFloat x4 = to.x +toOffset.dx;
CGFloat y4 = to.y +toOffset.dy;
CGFloat intersectionX = ((x1*y2-y1*x2)*(x3-x4) - (x1-x2)*(x3*y4-y3*x4)) / ((x1-x2)*(y3-y4) - (y1-y2)*(x3-x4));
CGFloat intersectionY = ((x1*y2-y1*x2)*(y3-y4) - (y1-y2)*(x3*y4-y3*x4)) / ((x1-x2)*(y3-y4) - (y1-y2)*(x3-x4));
CGPoint intersection = CGPointMake(intersectionX, intersectionY);
CornerPoint corner;
corner.centerPoint = intersection;
corner.startAngle = fromAngle - M_PI_2;
corner.endAngle = toAngle - M_PI_2;
return corner;
}
然后我使用该代码3次来计算3个角:
CornerPoint leftCorner = [self roundedCornerWithLinesFrom:right
via:left
to:top
withRadius:radius];
CornerPoint topCorner = [self roundedCornerWithLinesFrom:left
via:top
to:right
withRadius:radius];
CornerPoint rightCorner = [self roundedCornerWithLinesFrom:top
via:right
to:left
withRadius:radius];
现在,拥有所有必要的数据,启动我们创建实际路径的部分。我将依赖于CGPathAddArc将从当前点添加到起点的直线而不必自己绘制这些行(这是记录的行为)。
我手动必须计算的唯一点是路径的起点。我选择右下角的开头(没有具体原因)。从那里你只需添加一个圆弧,中心位于起点和终点的交点:
CGMutablePathRef roundedTrianglePath = CGPathCreateMutable();
// manually calculated start point
CGPathMoveToPoint(roundedTrianglePath, NULL,
leftCorner.centerPoint.x + radius*cosf(leftCorner.startAngle),
leftCorner.centerPoint.y + radius*sinf(leftCorner.startAngle));
// add 3 arcs in the 3 corners
CGPathAddArc(roundedTrianglePath, NULL,
leftCorner.centerPoint.x, leftCorner.centerPoint.y,
radius,
leftCorner.startAngle, leftCorner.endAngle,
NO);
CGPathAddArc(roundedTrianglePath, NULL,
topCorner.centerPoint.x, topCorner.centerPoint.y,
radius,
topCorner.startAngle, topCorner.endAngle,
NO);
CGPathAddArc(roundedTrianglePath, NULL,
rightCorner.centerPoint.x, rightCorner.centerPoint.y,
radius,
rightCorner.startAngle, rightCorner.endAngle,
NO);
// close the path
CGPathCloseSubpath(roundedTrianglePath);
看起来像这样:
没有所有支持线的最终结果如下:
答案 1 :(得分:34)
@ David的几何图形很酷且很有教育意义。但是你真的不需要以这种方式完成整个几何体。我将提供一个更简单的代码:
CGFloat radius = 20;
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, (center.x + bottomLeft.x) / 2, (center.y + bottomLeft.y) / 2);
CGPathAddArcToPoint(path, NULL, bottomLeft.x, bottomLeft.y, bottomRight.x, bottomRight.y, radius);
CGPathAddArcToPoint(path, NULL, bottomRight.x, bottomRight.y, center.x, center.y, radius);
CGPathAddArcToPoint(path, NULL, center.x, center.y, bottomLeft.x, bottomLeft.y, radius);
CGPathCloseSubpath(path);
UIBezierPath *bezierPath = [UIBezierPath bezierPathWithCGPath:path];
CGPathRelease(path);
bezierPath
就是您所需要的。关键是CGPathAddArcToPoint
为你做几何。
答案 2 :(得分:14)
基于上面的@ an0 answer:
override func viewDidLoad() {
let triangle = CAShapeLayer()
triangle.fillColor = UIColor.lightGray.cgColor
triangle.path = createRoundedTriangle(width: 200, height: 200, radius: 8)
triangle.position = CGPoint(x: 200, y: 300)
view.layer.addSublayer(triangle)
}
func createRoundedTriangle(width: CGFloat, height: CGFloat, radius: CGFloat) -> CGPath {
// Points of the triangle
let point1 = CGPoint(x: -width / 2, y: height / 2)
let point2 = CGPoint(x: 0, y: -height / 2)
let point3 = CGPoint(x: width / 2, y: height / 2)
let path = CGMutablePath()
path.move(to: CGPoint(x: 0, y: height / 2))
path.addArc(tangent1End: point1, tangent2End: point2, radius: radius)
path.addArc(tangent1End: point2, tangent2End: point3, radius: radius)
path.addArc(tangent1End: point3, tangent2End: point1, radius: radius)
path.closeSubpath()
return path
}
let cgPath = createRoundedTriangle(width: 200, height: 200, radius: 8)
let path = UIBezierPath(cgPath: cgPath)
答案 3 :(得分:2)
快速圆角三角形
根据@invisible squirrel 的回答,我编写了一个 UIView 扩展。
class TriangleView: UIView {
override func draw(_ rect: CGRect) {
let widthHeight = self.layer.frame.size.height
let triangle = CAShapeLayer()
triangle.fillColor = UIColor.black.cgColor
triangle.path = roundedTriangle(widthHeight: widthHeight)
triangle.position = CGPoint(x: 0, y: 0)
self.layer.addSublayer(triangle)
}
func roundedTriangle(widthHeight: CGFloat) -> CGPath {
let point1 = CGPoint(x: widthHeight/2, y:0)
let point2 = CGPoint(x: widthHeight , y: widthHeight)
let point3 = CGPoint(x: 0, y: widthHeight)
let path = CGMutablePath()
path.move(to: CGPoint(x: 0, y: widthHeight))
path.addArc(tangent1End: point1, tangent2End: point2, radius: 5)
path.addArc(tangent1End: point2, tangent2End: point3, radius: 5)
path.addArc(tangent1End: point3, tangent2End: point1, radius: 5)
path.closeSubpath()
return path
}
}
答案 4 :(得分:1)
@ an0 - 谢谢!我是全新的,所以我没有声称有用或评论的声誉,但你今天节省了我很多时间。
我知道这超出了问题的范围,但同样的逻辑适用于CGContextBeginPath()
,CGContextMoveToPoint()
,CGContextAddArcToPoint()
和CGContextClosePath()
,以防有人需要请改用它。
如果有人有兴趣,这是我的等边三角形指向右边的代码。 triangleFrame
是预定义的CGRect
,radius
是预定义的CGFloat
。
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextBeginPath(context);
CGContextMoveToPoint(context, CGRectGetMinX(triangleFrame), CGRectGetMidY(triangleFrame)); CGRectGetMidY(triangleFrame));
CGContextAddArcToPoint(context, CGRectGetMinX(triangleFrame), CGRectGetMidY(triangleFrame), CGRectGetMinX(triangleFrame), CGRectGetMinY(triangleFrame), radius);
CGContextAddArcToPoint(context, CGRectGetMinX(triangleFrame), CGRectGetMinY(triangleFrame), CGRectGetMaxX(triangleFrame), CGRectGetMidY(triangleFrame), radius);
CGContextAddArcToPoint(context, CGRectGetMaxX(triangleFrame), CGRectGetMidY(triangleFrame), CGRectGetMinX(triangleFrame), CGRectGetMaxY(triangleFrame), radius);
CGContextAddArcToPoint(context, CGRectGetMinX(triangleFrame), CGRectGetMaxY(triangleFrame), CGRectGetMinX(triangleFrame), CGRectGetMidY(triangleFrame), radius);
当然,可以针对不规则三角形更具体地定义每个点。然后,您可以根据需要CGContextFillPath()
或CGContextStrokePath()
。
答案 5 :(得分:0)
基于@ David的answer我写了一个小UIBezierPath
扩展,添加了与CGPath相同的功能:
extension UIBezierPath {
// Based on https://stackoverflow.com/a/20646446/634185
public func addArc(from startPoint: CGPoint,
to endPoint: CGPoint,
centerPoint: CGPoint,
radius: CGFloat,
clockwise: Bool) {
let fromAngle = atan2(centerPoint.y - startPoint.y,
centerPoint.x - startPoint.x)
let toAngle = atan2(endPoint.y - centerPoint.y,
endPoint.x - centerPoint.x)
let fromOffset = CGVector(dx: -sin(fromAngle) * radius,
dy: cos(fromAngle) * radius)
let toOffset = CGVector(dx: -sin(toAngle) * radius,
dy: cos(toAngle) * radius)
let x1 = startPoint.x + fromOffset.dx
let y1 = startPoint.y + fromOffset.dy
let x2 = centerPoint.x + fromOffset.dx
let y2 = centerPoint.y + fromOffset.dy
let x3 = centerPoint.x + toOffset.dx
let y3 = centerPoint.y + toOffset.dy
let x4 = endPoint.x + toOffset.dx
let y4 = endPoint.y + toOffset.dy
let intersectionX =
((x1 * y2 - y1 * x2) * (x3 - x4) - (x1 - x2) * (x3 * y4 - y3 * x4))
/ ((x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4))
let intersectionY =
((x1 * y2 - y1 * x2) * (y3 - y4) - (y1 - y2) * (x3 * y4 - y3 * x4))
/ ((x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4))
let intersection = CGPoint(x: intersectionX, y: intersectionY)
let startAngle = fromAngle - .pi / 2.0
let endAngle = toAngle - .pi / 2.0
addArc(withCenter: intersection,
radius: radius,
startAngle: startAngle,
endAngle: endAngle,
clockwise: clockwise)
}
}
因此,代码应该是这样的:
let path = UIBezierPath()
let center = CGPoint(x: rect.size.width / 2, y: 0)
let bottomLeft = CGPoint(x: 10, y: rect.size.height - 0)
let bottomRight = CGPoint(x: rect.size.width - 0, y: rect.size.height - 0)
path.move(to: center);
path.addArc(from: center,
to: bottomRight,
centerPoint: bottomLeft,
radius: radius,
clockwise: false)
path.addLine(to: bottomRight)
path.close()
答案 6 :(得分:-2)
更简单的是,只需添加两行代码即可:
UIBezierPath *bezierPath = [UIBezierPath bezierPath];
bezierPath.lineCapStyle = kCGLineCapRound;
bezierPath.lineJoinStyle = kCGLineJoinRound;
CGPoint center = CGPointMake(rect.size.width / 2, 0);
...
修改强>
要看到角是圆角的,你需要使三角形比矩形边界小一点:
CGPoint center = CGPointMake(rect.size.width / 2, 10);
CGPoint bottomLeft = CGPointMake(10, rect.size.height - 10);
CGPoint bottomRight = CGPointMake(rect.size.width - 10, rect.size.height - 10);