- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我需要在我的 Draw 应用程序中用箭头画线。我三角学不好,所以解决不了这个问题。
用户将手指放在屏幕上并向任意方向画线。所以,箭头应该出现在行尾。
最佳答案
I've posted a Swift version of this answer separately.
这是一个有趣的小问题。首先,有很多方法来绘制箭头,有弯曲的或直的。让我们选择一种非常简单的方法并标记我们需要的测量值:
我们想写一个函数,它接受起点、终点、尾部宽度、头部宽度和头部长度,并返回一个勾勒出箭头形状的路径。让我们创建一个名为 dqd_arrowhead
的类别来将此方法添加到 UIBezierPath
:
// UIBezierPath+dqd_arrowhead.h
@interface UIBezierPath (dqd_arrowhead)
+ (UIBezierPath *)dqd_bezierPathWithArrowFromPoint:(CGPoint)startPoint
toPoint:(CGPoint)endPoint
tailWidth:(CGFloat)tailWidth
headWidth:(CGFloat)headWidth
headLength:(CGFloat)headLength;
@end
由于箭头的路径上有七个角,让我们通过命名该常量来开始我们的实现:
// UIBezierPath+dqd_arrowhead.m
#import "UIBezierPath+dqd_arrowhead.h"
#define kArrowPointCount 7
@implementation UIBezierPath (dqd_arrowhead)
+ (UIBezierPath *)dqd_bezierPathWithArrowFromPoint:(CGPoint)startPoint
toPoint:(CGPoint)endPoint
tailWidth:(CGFloat)tailWidth
headWidth:(CGFloat)headWidth
headLength:(CGFloat)headLength {
好的,简单的部分完成了。现在,我们如何找到路径上这七个点的坐标?如果箭头沿 X 轴对齐,则更容易找到点:
计算轴对齐箭头上的点坐标非常容易,但我们需要箭头的总长度来计算。我们将使用标准库中的 hypotf
函数:
CGFloat length = hypotf(endPoint.x - startPoint.x, endPoint.y - startPoint.y);
我们将调用一个辅助方法来实际计算这七个点:
CGPoint points[kArrowPointCount];
[self dqd_getAxisAlignedArrowPoints:points
forLength:length
tailWidth:tailWidth
headWidth:headWidth
headLength:headLength];
但我们需要转换这些点,因为通常我们不试图创建一个轴对齐的箭头。幸运的是,Core Graphics 支持一种称为仿射变换的变换,它可以让我们旋转和平移(滑动)点。我们将调用另一个辅助方法来创建将轴对齐的箭头转换为我们要求的箭头的转换:
CGAffineTransform transform = [self dqd_transformForStartPoint:startPoint
endPoint:endPoint
length:length];
现在我们可以使用轴对齐箭头的点和将其变成我们想要的箭头的变换来创建 Core Graphics 路径:
CGMutablePathRef cgPath = CGPathCreateMutable();
CGPathAddLines(cgPath, &transform, points, sizeof points / sizeof *points);
CGPathCloseSubpath(cgPath);
最后,我们可以在 CGPath
周围包裹一个 UIBezierPath
并返回它:
UIBezierPath *uiPath = [UIBezierPath bezierPathWithCGPath:cgPath];
CGPathRelease(cgPath);
return uiPath;
}
这是计算点坐标的辅助方法。这很简单。如果需要,请引用轴对齐箭头的图表。
+ (void)dqd_getAxisAlignedArrowPoints:(CGPoint[kArrowPointCount])points
forLength:(CGFloat)length
tailWidth:(CGFloat)tailWidth
headWidth:(CGFloat)headWidth
headLength:(CGFloat)headLength {
CGFloat tailLength = length - headLength;
points[0] = CGPointMake(0, tailWidth / 2);
points[1] = CGPointMake(tailLength, tailWidth / 2);
points[2] = CGPointMake(tailLength, headWidth / 2);
points[3] = CGPointMake(length, 0);
points[4] = CGPointMake(tailLength, -headWidth / 2);
points[5] = CGPointMake(tailLength, -tailWidth / 2);
points[6] = CGPointMake(0, -tailWidth / 2);
}
计算仿射变换更复杂。这就是三角函数的用武之地。您可以使用 atan2
和 CGAffineTransformRotate
和 CGAffineTransformTranslate
函数来创建它,但如果你记得足够多的三角函数,您可以直接创建它。咨询“The Math Behind the Matrices” in the Quartz 2D Programming Guide有关我在这里做什么的更多信息:
+ (CGAffineTransform)dqd_transformForStartPoint:(CGPoint)startPoint
endPoint:(CGPoint)endPoint
length:(CGFloat)length {
CGFloat cosine = (endPoint.x - startPoint.x) / length;
CGFloat sine = (endPoint.y - startPoint.y) / length;
return (CGAffineTransform){ cosine, sine, -sine, cosine, startPoint.x, startPoint.y };
}
@end
我已将所有代码放入 a gist for easy copy'n'paste .
有了这个分类,你就可以轻松画箭头了:
由于您只是生成一条路径,因此您可以选择不填充它,或者像本例中那样不描边:
不过,你必须小心。如果您使头部宽度小于尾部宽度,或者头部长度大于总箭头长度,则此代码不会阻止您获得时髦的结果:
关于objective-c - 如何使用 Core Graphics 绘制箭头?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13528898/
我有一个 PSD 布局要开发,还有一些东西,比如“卡片”,我很好奇我是否可以在没有图像的情况下只使用 CSS3 边框来做到这一点。 http://i.imgur.com/NSZYPsh.png (这些
我想添加一个轻量级的导航栏来切换登录和注册。结果应如下所示: 箭头应指示当前所选页面。 最佳答案 引用这个question ,问题中的 UI 与您的相似,并且可以根据您的需要调整答案中使用的概念。 关
我想创建一个元组,它包含一个箭头和一个描述箭头的字符串。如果我使用函数(而不是箭头)这样做,则以下工作如预期: funTimes10 = (*10) describe10 = "times 10" t
var std_obj = { activeEffect : 'fade', name : 'Jane', displayMe() { const doSomeEffects =
我有一个使用TActionToolBar和TActionManager的工具栏。一个按钮具有子按钮,这些子按钮可通过单击按钮右侧的向下小箭头来使用。 “向下箭头”按钮的宽度非常薄,需要精确的鼠标控制。
我正在使用 Javascript 进行流网络可视化。顶点表示为圆圈,边表示为箭头。 这是我的 Edge 类: function Edge(u, v) { this.u = u; // start
这个问题已经有答案了: Show border triangle in navbar using CSS (3 个回答) 已关闭 7 年前。 我有一个列表菜单,其边框宽度为 1px ...100%。
有什么方法可以从 javafx 2.2 表列中的排序表列中删除排序指示符/箭头吗? 最佳答案 这可以通过 css 来完成 .table-view .arrow { -fx-background
在我的应用程序中,我使用了 googlemap。在那,我在一个特定的位置放置了一个物体(自行车或汽车)。然后我搬到了其他地方。现在,根据我当前的位置,我需要显示一个箭头(校园),即我放置汽车或自行车的
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and t
我一直在 goole 中搜索如何仅使用 css 创建箭头和框。我在这里找到了一个近乎完美的例子:- http://dabblet.com/gist/4639593 如何更改此代码,使箭头指向左而不是右
嗨,我正在为这个问题挠头。我想创建一个如图所示的 CSS 渐变箭头,并能够将红色部分的填充定义为百分比。红色 block 只是纯色。 从这里JFiddle示例 我在创建绿色箭头方面取得了一些进展,但三
我刚刚开始在 Android 中创建一些自定义 View ,但在圆外绘制位图(箭头)时遇到问题。 这是我的代码: Canvas osCanvas = new Canvas(windowFrame);
我正在尝试绘制一个具有渐变的左侧箭头。这是我正在使用的代码,但我不明白为什么它不起作用。 .left-arrow{ position: relative; &:after{ rig
是否可以使用 GD 在 PHP 中创建此图像?我知道我需要使用 GD 和 imagecreate、imagecolorallocate、imagedestroy 等...但我不知道如何做曲线 我需要用
我在我的区域 map 中有一个我正在使用的工具台 http://qtip2.com/ 我调用工具提示时的代码与这个问题中的相同Tooltip on map area tag jQuery(docume
我正在尝试在 Haskell 中学习 Arrows,所以我正在使用基于箭头的 HXT 库为 XML 编写一个简单的应用程序。 HXT wiki 和教程中的示例放弃了函数类型签名。但是,我非常喜欢类型,
我一直在使用 Haskell(特别是 Yampa)中的 Arrowized FRP 库,但我不太清楚如何进行“连续”切换。我的意思是信号通过信号函数(下面的 sf),它本身就是一个信号(如图像的上半部
我想要一个引用,清楚地说明 PHP 的箭头/方法调用运算符 (->) 在运算符绑定(bind)顺序方面的位置。 不幸的是,authoritative PHP manual page关于运算符优先级没有
如何隐藏 QScrollBar 箭头? 我需要隐藏在水平滚动条中。 我试图用 setStyleSheet 隐藏: setStyleSheet(" QScrollBar:left-arrow:horiz
我是一名优秀的程序员,十分优秀!