如何在圆盘内绘制箭头?
How to draw an arrow inside a disk?
我想在普通圆盘内画一个箭头。或者更准确地说,我想从圆盘上移除箭头的形状(我们可以透过圆盘看到绘制箭头的地方),像这样:
我怎样才能做到这一点?
更新:
到目前为止,我已经使用以下代码绘制了一个圆盘和一个箭头:
// 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()
但这不是我要找的,因为箭头在圆盘的顶部,所以显然我看不到圆盘后面的东西。
你需要用混合来填充你的箭头。尝试使用此方法绘制箭头 -
// 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)
我觉得没必要用代码画这么复杂的形状,可以用.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)
使用蒙版,你会得到透明的箭头。
自己试试吧~
我想在普通圆盘内画一个箭头。或者更准确地说,我想从圆盘上移除箭头的形状(我们可以透过圆盘看到绘制箭头的地方),像这样:
我怎样才能做到这一点?
更新:
到目前为止,我已经使用以下代码绘制了一个圆盘和一个箭头:
// 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()
但这不是我要找的,因为箭头在圆盘的顶部,所以显然我看不到圆盘后面的东西。
你需要用混合来填充你的箭头。尝试使用此方法绘制箭头 -
// 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)
我觉得没必要用代码画这么复杂的形状,可以用.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)
使用蒙版,你会得到透明的箭头。
自己试试吧~