2016-07-23 6 views
0

Ich verwende Swift v2.2 und habe ein Rechteck wie unten gezeigt gezeichnet, das ich füllen möchte, um dann etwas weißen Text anzuzeigen. Ich sehe jedoch, dass der Pfad geschlossen wurde, aber das Rechteck nicht gefüllt wird. Bitte helfen und danke für jede Eingabe.Bezier close Pfad wird nicht gefüllt

-Code

class InstructionArrowBorderView: UIView { 

    var lineWidth: CGFloat = 1 {didSet { setNeedsDisplay() } } 
    var instructionRectPathColor: UIColor = UIColor(red:13/255.0, green:118/255.0, blue:255/255.0, alpha: 1.0) { didSet { setNeedsDisplay() } } 

    override func drawRect(rect: CGRect) { 

     let instructionRectPath = UIBezierPath() 
     instructionRectPath.moveToPoint(CGPoint(x: bounds.minX, y: bounds.maxY - 50)) 
     instructionRectPath.addLineToPoint(CGPoint(x: bounds.minX, y: bounds.minY)) 
     instructionRectPath.moveToPoint(CGPoint(x: bounds.minX, y: bounds.minY)) 
     instructionRectPath.addLineToPoint(CGPoint(x: bounds.maxX, y: bounds.minY)) 
     instructionRectPath.moveToPoint(CGPoint(x: bounds.maxX, y: bounds.minY)) 
     instructionRectPath.addLineToPoint(CGPoint(x: bounds.maxX, y: bounds.maxY - 50)) 
     instructionRectPath.moveToPoint(CGPoint(x: bounds.maxX, y: bounds.maxY - 50)) 
     instructionRectPath.addLineToPoint(CGPoint(x: bounds.minX, y: bounds.maxY - 50)) 
     instructionRectPath.lineWidth = lineWidth 
     instructionRectPath.closePath() 
     instructionRectPath.fill() 
     instructionRectPathColor.set() 
     instructionRectPath.stroke() 
    } 
} 

Layout-

  • Handy

    • Ansicht (wo Bezier Pfad gezeichnet wird)

      • beschriftet (mit weißen Text)

Ergebnis enter image description here

+1

Sollten Sie nicht rufen 'instructionRectPathColor.set()' 'vor instructionRectPath.fill () '? – beyowulf

+1

Bevor Sie '.fill()' aufrufen, setzen Sie keine Farbe. Versuchen Sie, eine Codezeile nach unten zu bewegen, und Sie sollten sehen, dass sie dieselbe Farbe wie der Rahmen aufweist. Im Moment füllt es sich nur mit der Standardfarbe, die klar ist. – NSGangster

Antwort

0

Du Einführung Diskontinuitäten in Ihren Weg. Wenn Sie addLineToPoint sagen, müssen Sie nicht auch moveToPoint sagen. Dies verhindert, dass der Pfad korrekt ausgefüllt wird. Sie möchten auch die Farbe füllen/Strich Farbe dann füllen/streichen den Pfad.

Ihr drawRect Code sollte sein:

let instructionRectPath = UIBezierPath() 
instructionRectPath.moveToPoint(CGPoint(x: bounds.minX, y: bounds.maxY - 50)) 
instructionRectPath.addLineToPoint(CGPoint(x: bounds.minX, y: bounds.minY)) 
instructionRectPath.addLineToPoint(CGPoint(x: bounds.maxX, y: bounds.minY)) 
instructionRectPath.addLineToPoint(CGPoint(x: bounds.maxX, y: bounds.maxY - 50)) 
instructionRectPath.addLineToPoint(CGPoint(x: bounds.minX, y: bounds.maxY - 50)) 
instructionRectPath.closePath() 
instructionRectPath.lineWidth = lineWidth 
instructionRectPathColor.set() 
instructionRectPath.fill() 
instructionRectPath.stroke() 

die mehr geschrieben werden konnte sogar knapp wie:

let instructionRectPath = UIBezierPath(rect: CGRect(x: bounds.minX, y: bounds.minY, width: bounds.maxX, height: bounds.maxY - 50)) 
instructionRectPath.lineWidth = lineWidth 
instructionRectPathColor.set() 
instructionRectPath.fill() 
instructionRectPath.stroke()