- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想绕过这个的每个角落。我使用 UIBezierPath 来绘制和设置
path.lineCapStyle = kCGLineCapRound;
path.lineJoinStyle = kCGLineJoinRound;
那行不通。
我有一个方便的方法来代替一系列的 addLineToPoint
和 addArcWithCenter
在自定义 View 中
- (void)drawRect:(CGRect)rect {
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(100, 100)];
[path addLineToPoint:CGPointMake(150, 100)];
[path addLineToPoint:CGPointMake(150, 150)];
[path addLineToPoint:CGPointMake(200, 150)];
[path addLineToPoint:CGPointMake(200, 200)];
[path addLineToPoint:CGPointMake(250, 200)];
[path addLineToPoint:CGPointMake(250, 250)];
[path addLineToPoint:CGPointMake(100, 250)];
[path closePath];
path.lineCapStyle = kCGLineCapRound;
path.lineJoinStyle = kCGLineJoinRound;
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGContextSetFillColorWithColor(context, UIColor.greenColor.CGColor);
CGContextAddPath(context, path.CGPath);
CGContextFillPath(context);
CGContextRestoreGState(context);
}
在 View Controller 中
- (void)viewDidLoad {
[super viewDidLoad];
testvvv *test = [[testvvv alloc] initWithFrame:self.view.bounds];
[self.view addSubview:test];
}
最佳答案
我猜你想要这个:
UIBezierPath
上没有用于圆角的 Core Graphics 函数。它们是 CGContextAddArcToPoint
和 CGPathAddArcToPoint
。我将解释它们的工作原理。
首先让我们考虑如何绘制两条线在尖角处相交。路径首次初始化时,其“当前点”位于原点:
您使用 CGPathMoveToPoint
移动到第一行的开头 (x0, y0)
:
然后使用CGPathAddLineToPoint
绘制第一条线,在拐角处结束于(x1, y1)
:
最后,您再次使用 CGPathAddLineToPoint
将第二条线绘制到 (x2, y2)
:
现在让我们使用 CGPathAddArcToPoint
代替圆角。倒回到刚刚使用 CGPathMoveToPoint
移动到第一行的开头:
CGPathAddArcToPoint
需要两个 点:拐角处的点(不会到达,因为它将绕过拐角)和形状中的下一个点角落。因此,您同时传递了 (x1,y1)
(角)和 (x2,y2)
(下一行的末尾)。您还传递给它角的半径:
请注意,CGPathAddArcToPoint
为您绘制了第一条线,并绘制了形成圆角的弧,并将当前点留在该弧的末端。因此,您随后使用 CGPathAddLineToPoint
绘制第二条线:
好的,这解释了(我希望)CGPathAddArcToPoint
是如何工作的。 CGContextAddArcToPoint
以相同的方式工作,但在上下文的路径上运行。
要将此应用于您的楼梯形状,您需要使用 CG*AddArcToPoint
函数来绘制每个角。由于您有一个封闭的形状并且不需要尖角,因此根本不需要使用 CG*AddLineToPoint
。您可以使用 arc 函数绘制每条线以及圆角。
需要注意的一件事是开始形状的位置。你不能从拐角处开始,因为那样的话你会从拐角处得到一条直线。相反,最简单的方法是从其中一条线的中点开始,远离任何角落。
此外,由于您是在 drawRect:
中执行此操作,因此您可以只使用上下文的路径而不是创建单独的 UIBezierPath
或 CGPath
对象。这是我用来绘制上面结果的代码:
- (void)drawRect:(CGRect)rect {
CGPoint p[] = {
{100, 100},
{150, 100},
{150, 150},
{200, 150},
{200, 200},
{250, 200},
{250, 250},
{100, 250},
};
size_t count = (sizeof p) / (sizeof p[0]);
CGPoint pLast = p[count - 1];
CGContextRef gc = UIGraphicsGetCurrentContext();
// Start at the midpoint of one of the lines.
CGContextMoveToPoint(gc, 0.5 * (pLast.x + p[0].x), 0.5 * (pLast.y + p[0].y));
for (size_t i = 1; i < count; ++i) {
CGContextAddArcToPoint(gc, p[i-1].x, p[i-1].y, p[i].x, p[i].y, 10);
}
CGContextAddArcToPoint(gc, pLast.x, pLast.y, p[0].x, p[0].y, 10);
// Connect the last corner's arc to the starting point.
CGContextClosePath(gc);
[UIColor.greenColor setFill];
CGContextFillPath(gc);
}
关于ios - 如何在不规则图形上添加圆角?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53076171/
negExpression : (NOT^)* primitiveElement ; 是我的规矩。我现在有这个代码: !!(1==1) 我希望我最终会得到这棵树: NOT | NOT
我遇到以下问题,我正在创建一个作为预算副本的表单,但这种类型的预算不包含增值税%,并且商品不会通过会计。 问题如下我创建了一个名为budget.table的模型如下: class TableEleme
我对 Java 相当陌生,但对一般编程不太熟悉。我在 Windows Vista 上使用 Java 1.7.0_07。我正在尝试弄清楚如何使 Swing Timer 定期计时。 我注意到,即使我设置了
我有一个静态站点,它突然显示不规则的标题。这是一个包含大量 JavaScript 的单一页面,包括页面顶部的表格选择。该网站六个月前运行良好。现在,我在 12 个不同的导航选项卡中的一半上看到无法解释
在我参加的 CS 类(class)中,有一个不规则语言的例子: {a^nb^n | n >= 0} 我可以理解它是不规则的,因为没有有限状态自动机/机器可以编写来验证和接受此输入,因为它缺少内存组件。
给定以下高频但稀疏的时间序列: #Sparse Timeseries dti1 = pd.date_range(start=datetime(2015,8,1,9,0,0),periods=10,fr
我有 X、Y、Z 格式的数据,其中所有数据都是一维数组,Z 是坐标 (X,Y) 处的测量幅度。我想将此数据显示为等高线或“imshow”图,其中等高线/颜色代表 Z 值(幅度)。 用于测量和 X 和
这是 Stackoverflow 上的一个递归问题,但给出的解决方案 here仍然不完美。对我来说,屈服仍然是 python 中最复杂的东西之一,所以我不知道如何自己修复它。 当给定函数的任何列表中的
我使用 PHP 5.3.3 在 RHEL 6 服务器上部署了一个 symfony 1.4 项目。我不定期地在 php 错误日志中收到条目,提示找不到 sfProjectConfiguration 并且
我是一名优秀的程序员,十分优秀!