2016-03-03 79 views
0

我有具體UIBezierPath,例如由Y切割的貝塞爾路徑協調

  override func drawInContext(ctx: CGContext) { 
    if let slider = slider { 
     // Clip 
     let rect = bounds.insetBy(dx: bounds.width/10, dy: bounds.height/2.2) 
     let path = UIBezierPath(roundedRect: rect, cornerRadius: 5) 
     let circleRadius : CGFloat = 10 
     let xCoordInset = bounds.width/10 
     let circlePath = UIBezierPath(ovalInRect: CGRectMake(xCoordInset , rect.midY - circleRadius, circleRadius * 2, circleRadius * 2)) 
     let circlePath1 = UIBezierPath(ovalInRect: CGRectMake(bounds.width - xCoordInset - circleRadius, rect.midY - circleRadius, circleRadius * 2, circleRadius * 2)) 
     let circlePath2 = UIBezierPath(ovalInRect: CGRectMake(rect.midX - circleRadius, rect.midY - circleRadius, circleRadius * 2, circleRadius * 2)) 

     path.appendPath(circlePath) 
     path.appendPath(circlePath1) 
     path.appendPath(circlePath2) 

     CGContextAddPath(ctx, path.CGPath) 

     CGContextSetFillColorWithColor(ctx, slider.trackTintColor.CGColor) 
     CGContextFillPath(ctx) 

    } 
} 

我想填補這一bezierPath與灰顏色的一半,另一半用紅色。所以我想我需要有2個相同的圖層,但是其中的1個應該用y座標來剪切,你能爲這個動作提供一些可用的方法嗎?

+0

你可以創建兩個相同的形狀,並應用其中一個面具 –

回答

0

在填充灰色之前剪切到切割線上方的矩形。然後剪切到切割線下面的矩形並填充紅色。

override func drawInContext(ctx: CGContext) { 
     if let slider = slider { 
      // Clip 
      let rect = bounds.insetBy(dx: bounds.width/10, dy: bounds.height/2.2) 
      let path = UIBezierPath(roundedRect: rect, cornerRadius: 5) 
      let circleRadius : CGFloat = 10 
      let xCoordInset = bounds.width/10 
      let circlePath = UIBezierPath(ovalInRect: CGRectMake(xCoordInset , rect.midY - circleRadius, circleRadius * 2, circleRadius * 2)) 
      let circlePath1 = UIBezierPath(ovalInRect: CGRectMake(bounds.width - xCoordInset - circleRadius, rect.midY - circleRadius, circleRadius * 2, circleRadius * 2)) 
      let circlePath2 = UIBezierPath(ovalInRect: CGRectMake(rect.midX - circleRadius, rect.midY - circleRadius, circleRadius * 2, circleRadius * 2)) 

      path.appendPath(circlePath) 
      path.appendPath(circlePath1) 
      path.appendPath(circlePath2) 

      let yCut = bounds.midY // or whatever 

      CGContextSaveGState(ctx); do { 
       CGContextClipToRect(ctx, CGRectMake(bounds.minX, bounds.minY, bounds.width, yCut - bounds.minY)) 
       CGContextAddPath(ctx, path.CGPath) 
       CGContextSetFillColorWithColor(ctx, slider.trackTintColor.CGColor) 
       CGContextFillPath(ctx) 
      }; CGContextRestoreGState(ctx) 

      CGContextSaveGState(ctx); do { 
       CGContextClipToRect(ctx, CGRectMake(bounds.minX, yCut, bounds.width, bounds.maxY - yCut)) 
       CGContextAddPath(ctx, path.CGPath) 
       CGContextSetFillColorWithColor(ctx, slider.progressTintColor.CGColor) 
       CGContextFillPath(ctx) 
      }; CGContextRestoreGState(ctx) 

     } 
    }