2014-08-27 3 views
11

나는 UIBezierPath의 인스턴스를 가지고 있으며 스트로크 색상을 검은 색이 아닌 다른 것으로 변경하려고합니다. 스위프트에서이 작업을 수행하는 방법을 아는 사람이 있습니까?Swift에서 UIBezierPath의 색상을 변경하는 방법은 무엇입니까?

+6

여기에 POB의 대답이 도움이되지만 베 지어 경로 자체에는 색상이 없음을 이해하는 것이 중요합니다. 이것은 곡선에 대한 설명입니다. 펜에만 색이 있으며 POB의 대답은 모두 펜 색을 설정하는 것입니다. –

답변

27

스위프트 3을 사용하면 UIColorsetStroke()입니다.

func setStroke() 

수신기가 나타내는 색으로 다음 스트로크 동작의 색상을 설정합니다 : setStroke()는 다음과 같은 선언이 있습니다.

따라서이 같은 setStroke()를 사용할 수 있습니다

strokeColor.setStroke() // where strokeColor is a `UIColor` instance 

아래의 놀이터 코드는 녹색 채우기 색과 빛으로 원을 그리려면 UIBezierPath 함께 setStroke()을 사용하는 방법을 보여줍니다 회색 계열 색상 UIView 하위 클래스 :

import UIKit 
import PlaygroundSupport 

class MyView: UIView { 

    override func draw(_ rect: CGRect) { 
     // UIBezierPath 
     let newRect = CGRect(
      x: bounds.minX + ((bounds.width - 79) * 0.5 + 0.5).rounded(.down), 
      y: bounds.minY + ((bounds.height - 79) * 0.5 + 0.5).rounded(.down), 
      width: 79, 
      height: 79 
     ) 
     let ovalPath = UIBezierPath(ovalIn: newRect) 

     // Fill 
     UIColor.green.setFill() 
     ovalPath.fill() 

     // Stroke 
     UIColor.lightGray.setStroke() 
     ovalPath.lineWidth = 5 
     ovalPath.stroke() 
    } 

} 

let myView = MyView(frame: CGRect(x: 0, y: 0, width: 200, height: 300)) 
PlaygroundPage.current.liveView = myView 
1

둥근 사각형을 칠하는 대신 빨강을 사용한다고 가정 해 봅시다. 위의 코드의

// Drawing the border of the rounded rectangle: 
    let redColor = UIColor.red 
    redColor.setStroke() // Stroke subsequent views with a red color 
    let roundedRectagle = CGRect(x: 0,y: 0, width: 90,height: 20) 
    let rectangleBorderPath = UIBezierPath(roundedRect: roundedRectangle,cornerRadius: 5) 
    roundedRectangle.borderWidth = 1 
    roundedRectangle.stroke() // Apply the red color stroke on this view 

두 번째와 마지막 줄이로 대답이 답변이 도움이 희망 대답에 중요하다 : 이는 스위프트 3에서 그것을 할 방법이다.

관련 문제