2017-06-08 30 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; 
} 
相关问题