2017-06-08 29 views
0

有誰知道如何從Xamarin c#中的NSBezierPath獲取CGPath?有一種方法可以在ObjC或Swift中進行轉換,但我相信Xamarin中可能存在一些問題,因爲我無法找到mutablepath類。將nsbezierpath轉換爲xamarin中的cgpath

private CGPath QuartzPath(NSBezierPath nsPath){ 
     nint j, numElements; 
     CGPath immutablePath = null; 
     numElements = nsPath.ElementCount; 

     if(numElements > 0){ 
      CGPath path = new CGPath(); 
      CGPoint[] points = new CGPoint[] { new CGPoint(0.0f, 0.0f),new CGPoint(0.0f, 0.0f),new CGPoint(0.0f, 0.0f) }; 
      bool didClosePath = true; 

      for (j = 0; j < numElements; j++){ 
       switch (nsPath.ElementAt(j)){ 
        case NSBezierPathElement.MoveTo: 
         path.MoveToPoint(points[0].X, points[0].Y); 
         break; 
        case NSBezierPathElement.LineTo: 
         path.AddLineToPoint(points[0].X, points[0].Y); 
         didClosePath = false; 
         break; 
        case NSBezierPathElement.CurveTo: 
         path.AddCurveToPoint(points[0].X, points[0].Y, points[1].X, points[1].Y, points[2].X, points[2].Y); 
         didClosePath = false; 
         break; 
        case NSBezierPathElement.ClosePath: 
         path.CloseSubpath(); 
         didClosePath = true; 
         break; 
       } 
      } 
      if(!didClosePath){ 
       path.CloseSubpath(); 
      } 
      immutablePath = path.Copy(); 
     } 
     return immutablePath; 
    } 

這個基本上是我的翻譯。在原始版本中,「path」變量被聲明爲mutablepath,但我在xamarin中找不到相應的類。那麼使用普通的CGPath可以嗎?

+0

爲什麼你不告訴我們你已經嘗試了什麼,並解釋你有什麼具體問題,而不是要求我們爲你解決問題?大多數ObjC - > Xamarin代碼轉換非常簡單。 – Jason

+0

@Jason非常感謝您提醒我!剛剛添加了我的代碼 – Xiangyu

+0

來回答你關於CGPath的問題,我認爲你是正確的,但只是測試它並看看會更容易。 ObjC區分Xamarin有時沒有的可變對象和不可變對象。 – Jason

回答

0
public static CGPath ToCGPath(this NSBezierPath nsPath) 
{ 
    var cgPath = new CGPath(); 
    var points = null as CGPoint[]; 

    for (int i = 0; i < nsPath.ElementCount; i++) 
    { 
     var type = nsPath.ElementAt(i, out points); 

     switch (type) 
     { 
      case NSBezierPathElement.MoveTo: 
       cgPath.MoveToPoint(points[0]); 
       break; 

      case NSBezierPathElement.LineTo: 
       cgPath.AddLineToPoint(points[0]); 
       break; 

      case NSBezierPathElement.CurveTo: 
       cgPath.AddCurveToPoint(cp1: points[0], cp2: points[1], point: points[2]); 
       break;  

      case NSBezierPathElement.ClosePath: 
       cgPath.CloseSubpath(); 
       break;  
     } 
    } 

    return cgPath; 
} 
相關問題