用UIKit 绘制虚线很容易.所以:
CGFloat dashes[] = {4, 2};
[path setLineDash:dashes count:2 phase:0];
[path stroke];
Run Code Online (Sandbox Code Playgroud)

有没有办法画出真正的虚线?

有任何想法吗?
由于这个问题很老,没有人提出完整的@IBDesignable解决方案,这里是...
希望能节省一些打字的人.
@IBDesignable class DottedVertical: UIView {
@IBInspectable var dotColor: UIColor = UIColor.etc
@IBInspectable var lowerHalfOnly: Bool = false
override func draw(_ rect: CGRect) {
// say you want 8 dots, with perfect fenceposting:
let totalCount = 8 + 8 - 1
let fullHeight = bounds.size.height
let width = bounds.size.width
let itemLength = fullHeight / CGFloat(totalCount)
let path = UIBezierPath()
let …Run Code Online (Sandbox Code Playgroud) 我能够使用以下代码绘制一个虚线框:
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
CGRect shapeRect = CGRectMake(0.0f, 0.0f, 200.0f, 100.0f);
[shapeLayer setBounds:shapeRect];
[shapeLayer setPosition:CGPointMake(self.coreImageView_.frameX, self.coreImageView_.frameBottom - self.coreImageView_.frameHeight/2)];
[shapeLayer setFillColor:[[UIColor clearColor] CGColor]];
[shapeLayer setStrokeColor:[[UIColor whiteColor] CGColor]];
[shapeLayer setLineWidth:2.0f];
[shapeLayer setLineJoin:kCALineJoinRound];
[shapeLayer setLineDashPattern:
[NSArray arrayWithObjects:[NSNumber numberWithInt:5],
[NSNumber numberWithInt:5],
nil]];
Run Code Online (Sandbox Code Playgroud)
现在如果我想从点X到点B绘制一条虚线,我该如何修改这段代码呢?
我可以UIBezierPath使用任何选定的字体和大小生成字符.现在我想在bezier路径之间做一条蚀刻线.我可以获得bezier路径的中心点吗?或者我可以制作中心虚线并沿着那条路走的任何其他方式?
这是我如何做的代码
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 568, 568) cornerRadius:0];
UIBezierPath *circlePath = [self createArcPath];
[path appendPath:circlePath];
[path setUsesEvenOddFillRule:YES];
shapeView = [CAShapeLayer layer];
shapeView.geometryFlipped = false;
shapeView.path = path.CGPath;
shapeView.fillRule = kCAFillRuleEvenOdd;
shapeView.fillColor = [UIColor grayColor].CGColor;
shapeView.opacity = 1.0;
shapeView.lineDashPattern = @[@2, @3];
[self.view.layer addSublayer:shapeView];
CGFloat dashes[] = {2, 3};
[path setLineDash:dashes count:2 phase:0];
- (UIBezierPath *)createArcPath
{
// Create path from text
// See: http://www.codeproject.com/KB/iPhone/Glyph.aspx
// License: The Code Project Open License (CPOL) 1.02 …Run Code Online (Sandbox Code Playgroud) 我用这个例子在上画了虚线uiview:
UIBezierPath *path = [UIBezierPath bezierPath];
//draw a line
[path moveToPoint:yourStartPoint]; //add yourStartPoint here
[path addLineToPoint:yourEndPoint];// add yourEndPoint here
[path stroke];
float dashPattern[] = {1,1,1,1}; //make your pattern here
[path setLineDash:dashPattern count:4 phase:0];
UIColor *fill = [UIColor blueColor];
shapelayer.strokeStart = 0.0;
shapelayer.strokeColor = fill.CGColor;
shapelayer.lineWidth = 7.0;
shapelayer.lineJoin = kCALineJoinMiter;
shapelayer.lineDashPattern = [NSArray arrayWithObjects:[NSNumber numberWithInt:10],[NSNumber numberWithInt:10], nil];
shapelayer.lineDashPhase = 3.0f;
shapelayer.path = path.CGPath;
Run Code Online (Sandbox Code Playgroud)
它有效,但问题是即使我将uiview高度设置为 1,我得到的线条也很粗。

有没有可能让它更薄?
ios ×4
objective-c ×3
swift ×2
ipad ×1
iphone ×1
nsbezierpath ×1
uibezierpath ×1
uikit ×1
uiview ×1