2016-07-23 7 views
0

私はSwift v2.2を使用しています。下に示すように描画して、次に白いテキストを表示します。しかし、パスが閉じているが、長方形が塗りつぶされていないことがわかります。何か助けていただきありがとうございます。ベジエ近道が塗りつぶされていない

コード

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() 
    } 
} 

レイアウト

  • セル(ベジェのパスが描かれている)

    • ビュー

      • (白いテキスト付き)ラベル

結果 enter image description here

+1

は、あなたが呼び出すべきではありません 'instructionRectPathColor.set()'(instructionRectPath.fill '前) '? – beyowulf

+1

あなたが '.fill()'を呼び出す前に色を設定していないのであれば、それをコード行の下に移動してみてください。境界線と同じ色で塗りつぶされるはずです。今はちょうどクリアなデフォルトの色で塗りつぶしています。 – NSGangster

答えて

0

あなたはあなたのパスに不連続性を導入しています。 addLineToPointと言っても、moveToPointと言う必要はありません。これにより、パスが正しく入力されなくなります。また、色の塗りつぶし/ストロークの色を設定し、パスを塗りつぶす/ストロークする必要があります。

あなたdrawRectコードは次のようになります。でも、もっと簡潔のように書くことができ

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() 

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() 
関連する問題