【发布时间】:2015-12-22 12:32:03
【问题描述】:
我正在尝试了解如何使用 Swift 创建三角形。我发现这段代码创建了一个三角形。
class TriangleLayer: CAShapeLayer {
let innerPadding: CGFloat = 30.0
override init() {
super.init()
fillColor = Colors.red.CGColor
strokeColor = Colors.red.CGColor
lineWidth = 7.0
lineCap = kCALineCapRound
lineJoin = kCALineJoinRound
path = trianglePathSmall.CGPath
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
var trianglePathSmall: UIBezierPath {
let trianglePath = UIBezierPath()
trianglePath.moveToPoint(CGPoint(x: 5.0 + innerPadding, y: 95.0)) // #1
trianglePath.addLineToPoint(CGPoint(x: 50.0, y: 12.5 + innerPadding)) // #2
trianglePath.addLineToPoint(CGPoint(x: 95.0 - innerPadding, y: 95.0)) // #3
trianglePath.closePath()
return trianglePath
}
我尝试对其进行调整和尝试以了解它是如何工作的;但是,在这一点上,我意识到我对逻辑迷失了很多。我将上面三角形的 CGPoints 放在我头上的 x-y 轴上,看起来像:
#1 x:35, y:95 #3 x:65, y:95
#2 x:50, y: 42.5
但是如果我将点放在 x-y 轴上,三角形就会倒置。
我想要实现的就是轴所告诉的,我想要实现..
. . .
<like this. not this>
. . .
【问题讨论】:
-
developer.apple.com/library/mac/documentation/GraphicsImaging/… 这是 Apple 的二维绘图文档。
-
0,0 在左上角,所以 Y 轴向下递增。
标签: ios swift uibezierpath cashapelayer