将Objective C转换为Monotouch(圆形表格分组)

时间:2015-06-07 13:58:49

标签: xamarin xamarin.ios monotouch.dialog

我有以下Objective C代码:

CGFloat cornerRadius = 5.f;
cell.backgroundColor = UIColor.clearColor;
CAShapeLayer *layer = [[CAShapeLayer alloc] init];
CGMutablePathRef pathRef = CGPathCreateMutable();
CGRect bounds = CGRectInset(cell.bounds, 10, 0);
BOOL addLine = NO;
if (indexPath.row == 0 && indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) {
    CGPathAddRoundedRect(pathRef, nil, bounds, cornerRadius, cornerRadius);
} else if (indexPath.row == 0) {
    CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds));
    CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds), CGRectGetMidX(bounds), CGRectGetMinY(bounds), cornerRadius);
    CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius);
    CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds));
    addLine = YES;
} else if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) {
    CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds));
    CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds), CGRectGetMidX(bounds), CGRectGetMaxY(bounds), cornerRadius);
    CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius);
    CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds));
} else {
    CGPathAddRect(pathRef, nil, bounds);
    addLine = YES;
}
layer.path = pathRef;
CFRelease(pathRef);
layer.fillColor = [UIColor colorWithWhite:1.f alpha:0.8f].CGColor;

if (addLine == YES) {
    CALayer *lineLayer = [[CALayer alloc] init];
    CGFloat lineHeight = (1.f / [UIScreen mainScreen].scale);
    lineLayer.frame = CGRectMake(CGRectGetMinX(bounds)+10, bounds.size.height-lineHeight, bounds.size.width-10, lineHeight);
    lineLayer.backgroundColor = tableView.separatorColor.CGColor;
    [layer addSublayer:lineLayer];
}
UIView *testView = [[UIView alloc] initWithFrame:bounds];
[testView.layer insertSublayer:layer atIndex:0];
testView.backgroundColor = UIColor.clearColor;
cell.backgroundView = testView;

我翻译成C#(Xamarin):

float cornerRadius = 5f;
cell.BackgroundColor = UIColor.Clear;
CAShapeLayer layer = new CAShapeLayer ();
CGPath pathRef = new CGPath();

RectangleF bounds =  new RectangleF(cell.Bounds.X,cell.Bounds.Y, 5, 0);
bool addLine = false;

if (indexPath.Row == 0 && indexPath.Row == tableView.NumberOfRowsInSection(indexPath.Section)-1) {
    pathRef.AddRoundedRect (bounds, cornerRadius, cornerRadius);

} else if (indexPath.Row == 0) {
    pathRef.MoveToPoint (bounds.GetMinX(), bounds.GetMaxY());
    pathRef.AddArcToPoint(bounds.GetMinX(),bounds.GetMinY(), bounds.GetMidX(),bounds.GetMinY(),cornerRadius);
    pathRef.AddArcToPoint(bounds.GetMaxX(),bounds.GetMinY(), bounds.GetMaxX(),bounds.GetMidY(),cornerRadius);
    pathRef.AddLineToPoint(bounds.GetMaxX(),bounds.GetMaxY());

    addLine = true;
} else if (indexPath.Row == tableView.NumberOfRowsInSection(indexPath.Section)-1) {

    pathRef.MoveToPoint (bounds.GetMinX(), bounds.GetMinY());
    pathRef.AddArcToPoint(bounds.GetMinX(),bounds.GetMaxY(), bounds.GetMidX(),bounds.GetMaxY(),cornerRadius);
    pathRef.AddArcToPoint(bounds.GetMaxX(),bounds.GetMaxY(), bounds.GetMaxX(),bounds.GetMidY(),cornerRadius);
    pathRef.AddLineToPoint(bounds.GetMaxX(),bounds.GetMinY());

} else {
    pathRef.AddRect (bounds);

    addLine = true;
}

layer.Path = pathRef;

//CFRelease(pathRef);
layer.FillColor = new CGColor (1f, 0.8f);// UIColor.FromWhiteAlpha (1f, 0.8f);

if (addLine) {
    CALayer lineLayer = new CALayer ();
    float lineHeight = (1f / UIScreen.MainScreen.Scale);
    lineLayer.Frame = new RectangleF (bounds.GetMinX () + 10, bounds.Size.Height - lineHeight, bounds.Size.Width - 10, lineHeight);
    lineLayer.BackgroundColor = tableView.SeparatorColor.CGColor;
    layer.AddSublayer (lineLayer);
}
UIView testView = new UIView (bounds);
testView.Layer.InsertSublayer (layer, 0);
testView.BackgroundColor = UIColor.Clear;
cell.BackgroundView = testView;

但它现在正在运行,我不会在构建时让应用程序崩溃也不会出错。

我想从这个问题中实现以下目标:

iOS 7 TableView like in Settings App on iPad

感谢任何帮助。

0 个答案:

没有答案