2016-11-19 19 views
0

UIBezierPathセクションごとに異なるストロークカラーを設定したいとします。しかし、命令は完全に間違っており、私はそれを修正する方法を知らない。UIGraphicsGetCurrentContextごとに異なるsetStrokeカラー

enter image description here

そして、これは私が得るものです::

これは私が欲しいものである

enter image description here

順序が間違っているように思えます。 bezierPathに色をバインドしてコンテキストに追加する方法はありますか?私のコードは以下の通りです。ありがとう!

 let size = CGSize(width: 134, height:51) 
    UIGraphicsBeginImageContext(size) 
    let context = UIGraphicsGetCurrentContext() 

    //// Rectangle Drawing 
    let rectanglePath = UIBezierPath(roundedRect: CGRect(x: 0.5, y: 0.5, width: 126, height: 50), cornerRadius: 3) 
    UIColor.lightGray.setStroke() 
    rectanglePath.lineWidth = 1 
    rectanglePath.stroke() 
    let clipPath: CGPath = rectanglePath.cgPath 
    context?.addPath(clipPath) 

    //// Rectangle 2 Drawing 
    let rectangle2Path = UIBezierPath(roundedRect: CGRect(x: 3, y: 3, width: 121, height: 45), cornerRadius: 3) 
    UIColor.green.setFill() 
    rectangle2Path.fill() 
    let clipPathh: CGPath = rectangle2Path.cgPath 
    context?.addPath(clipPathh) 

    let rectangle3Path = UIBezierPath(roundedRect: CGRect(x: 128, y: 18, width: 6, height: 14), byRoundingCorners: [.topRight, .bottomRight], cornerRadii: CGSize(width: 3, height: 3)) 
    UIColor.gray.setFill() 
    rectangle3Path.fill() 
    let clipPathhh: CGPath = rectangle3Path.cgPath 
    context?.addPath(clipPathhh) 

    context?.closePath() 

    // Convert to UIImage 
    let cgimage = context!.makeImage(); 
    let uiimage = UIImage(cgImage: cgimage!) 

    // End the graphics context 
    UIGraphicsEndImageContext() 

    image.image = uiimage; 
+0

私はIBを使って投げ込まれたUIImageViewを使って、あなたのコードを新しいプロジェクトにコピーして貼り付けました。私は正しい結果を得ています。緑色の塗りつぶしを囲む明るい灰色の境界なぜそれが私のために働くかわからない。あなたが本当に望むのは、白い枠線の外に灰色の枠線があり、緑色の枠線がある場合は、白の第3の経路が必要な場合があります。 – dfd

+0

Mhh、それをviewDidLoadに貼り付けましたか? – da1lbi3

+0

はい。再び、粗くて素早いコピー/過去。ベジェパスの場合は、通常、ビューのdrawRect内のものが必要です。 – dfd

答えて

1

これが表示されます。ベジェパスで作業して以来、しばらくしていますが、周りを少し遊んでみると問題が見つかりました。すべてが順番です。コードは次のようになります。あなたは、コンテキストへのパスを追加しした後、充填/ストロークを行う

let size = CGSize(width: 134, height:51) 
UIGraphicsBeginImageContext(size) 
let context = UIGraphicsGetCurrentContext() 

//// Rectangle Drawing 
let rectanglePath = UIBezierPath(roundedRect: CGRect(x: 0.5, y: 0.5, width: 126, height: 50), cornerRadius: 3) 
UIColor.lightGray.setStroke() 
rectanglePath.lineWidth = 1 
let clipPath: CGPath = rectanglePath.cgPath 
context?.addPath(clipPath) 
rectanglePath.stroke() 

//// Rectangle 2 Drawing 
let rectangle2Path = UIBezierPath(roundedRect: CGRect(x: 3, y: 3, width: 121, height: 45), cornerRadius: 3) 

UIColor.green.setFill() 
let clipPathh: CGPath = rectangle2Path.cgPath 
context?.addPath(clipPathh) 
rectangle2Path.fill() 

let rectangle3Path = UIBezierPath(roundedRect: CGRect(x: 128, y: 18, width: 6, height: 14), byRoundingCorners: [.topRight, .bottomRight], cornerRadii: CGSize(width: 3, height: 3)) 
UIColor.gray.setFill() 
let clipPathhh: CGPath = rectangle3Path.cgPath 
context?.addPath(clipPathhh) 
rectangle3Path.fill() 

// Convert to UIImage 
let cgimage = context!.makeImage(); 
let uiimage = UIImage(cgImage: cgimage!) 

// End the graphics context 
UIGraphicsEndImageContext() 


imageView.image = uiimage; 

注意。また、rectPathを定義してパス全体を指定しているので、closePath呼び出しは影響を与えません。

関連する問題