如何在磁盘内绘制箭头?

时间:2015-06-16 12:02:32

标签: ios swift core-graphics uibezierpath

我想在普通磁盘内画一个箭头。或者更确切地说,我想从磁盘中删除箭头的形状(我们可以看到绘制箭头的磁盘),如下所示:

enter image description here

我怎样才能做到这一点?

更新:

到目前为止,我使用以下代码绘制了一个磁盘和一个箭头:

// Disk
var ovalPath = UIBezierPath(ovalInRect: CGRectMake(95, 62, 23, 23))
color2.setFill()
ovalPath.fill()


// Arrow
var bezierPath = UIBezierPath()
bezierPath.moveToPoint(CGPointMake(95.5, 75.5))
bezierPath.addLineToPoint(CGPointMake(106.5, 65.5))
bezierPath.addLineToPoint(CGPointMake(117.5, 75.5))
color3.setStroke()
bezierPath.lineWidth = 1
bezierPath.stroke()

但它并不是我所寻找的,因为箭头位于磁盘顶部,所以很明显我无法看到磁盘背后的内容。

2 个答案:

答案 0 :(得分:2)

我认为没有必要使用代码绘制这样复杂的形状,您可以使用.png资产来替换。

但如果你还想画它。以下是我在Xcode6.3 Playground中对CALayer所做的事情

import UIKit
import XCPlayground

let sequence = [
CGPointMake(56, 22),
CGPointMake(104, 78),
CGPointMake(99, 82),
CGPointMake(57, 30),
CGPointMake(16, 82),
CGPointMake(11, 78)
]


let diskLayer = CAShapeLayer()
var ovalPath = UIBezierPath(rect: CGRectMake(0, 0, 120, 120))
diskLayer.path = ovalPath.CGPath
diskLayer.fillColor = UIColor.redColor().CGColor

let arrowLayer = CAShapeLayer()
let arrowPath = CGPathCreateMutable()
CGPathMoveToPoint(arrowPath, nil, 11,78)
CGPathAddLines(arrowPath, nil, sequence, 6)
CGPathAddEllipseInRect(arrowPath, nil, CGRectMake(0, 0, 120, 120))


arrowLayer.path = arrowPath
arrowLayer.fillRule = kCAFillRuleEvenOdd
diskLayer.mask = arrowLayer


var view = UIView(frame: CGRectMake(0, 0, 120, 120))
view.layer.addSublayer(diskLayer)


XCPShowView("sample view", view)

enter image description here

使用蒙版,您将获得透明箭头。

亲自试试〜

答案 1 :(得分:1)

你需要用混合填充箭头。试试这个箭头图 -

// Arrow
var bezierPath = UIBezierPath()
bezierPath.moveToPoint(CGPointMake(95.5, 75.5))
bezierPath.addLineToPoint(CGPointMake(106.5, 65.5))
bezierPath.addLineToPoint(CGPointMake(117.5, 75.5))
UIColor.whiteColor().setFill()
bezierPath.fillWithBlendMode(kCGBlendModeSourceIn, alpha:1)