2017-05-09 3 views
0

슬라이더를 사용하여 드로잉 앱의 선 너비를 변경하려고하지만 슬라이더를 변경하고 선을 다시 그릴 때마다 모든 행이 화면에 나타납니다. 현재 선택된 선 너비로 변경하십시오. 나는 틀린 일을해야만합니다.스위프트 드로잉 앱 - 슬라이더를 기반으로 선 너비 값을 변경합니다.

var layers:[Array<CGPoint>] = [] 
var layerIndex = 0 
var sliderValue: CGFloat = 3.0 
var strokeInfo:[[String:Any]] = [] 

//change the slider 
func slider(value: CGFloat) { 
    sliderValue = value 
    print("Slider value is \(sliderValue)") 
} 

//on new touch, start a new array (layer) of points 
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
    var points:[CGPoint] = [] 
    layers.append(points) 
    var info = Dictionary<String,Any>() 
    info["line"] = sliderValue 
    strokeInfo.insert(info, at: layerIndex) 
    let pt = (touches.first!).location(in: self) 
    points.append(pt) 
} 


//add each point to the correct array as the finger moves 
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { 
    let pt = (touches.first!).location(in: self) 
    layers[layerIndex].append(pt) 
    self.setNeedsDisplay() 
} 


override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 
    print("layer \(layerIndex) now has \(layers[layerIndex].count)") 
    layerIndex += 1 
} 

override func draw(_ rect: CGRect) { 
    //get pointer to view 
    let context = UIGraphicsGetCurrentContext() 
    context?.clear(rect) 

    for (index, element) in strokeInfo.enumerated() { 
     if index == layerIndex { 
      context?.setLineWidth(element["line"] as! CGFloat) 
     } 
    } 

    //loop through the layers if any 
    for points in layers { 

     UIColor.cyan.set() 

     //loop through each layer's point values 
     if points.count - 1 > 0 { 

     for i in 0 ..< points.count-1 { 

      let pt1 = points[i] as CGPoint 
      let pt2 = points[i + 1] as CGPoint 

      context?.setLineCap(.round) 
      context?.move(to: pt1) 
      context?.addLine(to: pt2) 
      context?.strokePath() 
     } 
     } 
    } 
} 

답변

1

컨텍스트의 선 너비와 줄 바꿈이 변경됩니다. 그래픽 콘텍스트의 설정은 콘텍스트와 관련된 전체 경로에 영향을 미친다.

다른 경로를 그리려면 여러 UIBezierPath 개체를 사용하는 것이 좋습니다. 각각의 개체에는 너비, 색 및 라인 컷 설정이 있습니다. drawRect 메소드에서 베 지어 경로를 그릴 수 있습니다.

각기 다른 패스 드로잉 설정을 가진 여러 CAShapeLayers를 사용하여 서로 합성하여 원하는 합성 이미지를 얻을 수 있습니다.