【问题标题】:Draw triangle as subview to right corner of table cell将三角形作为子视图绘制到表格单元格的右角
【发布时间】:2023-03-19 06:30:01
【问题描述】:

我想使用 CGRect 而不是 UIImageView 绘制一个三角形,并将其作为子视图添加到某些特定表格单元格的右上角,类似于它在 WWDC 应用程序上的完成方式。

欢迎任何建议。 :)

【问题讨论】:

    标签: ios iphone objective-c tableview subview


    【解决方案1】:

    一种简单的方法是让不同的 UIImageView 包含一个彩色三角形,然后根据您设置的某些值/偏好选择要显示的不同图像。 取自这里:Drawing a triangle in UIView

    您需要做一些数学运算来计算正确的点,但这是绘制三角形的一般方法。您可以创建自己的 UIView 子类(称为 CornerTriangle),然后将其添加到单元格中,同时设置其颜色以满足您的需求。

    -(void)drawRect:(CGRect)rect
    {
        CGContextRef ctx = UIGraphicsGetCurrentContext();
    
        CGContextBeginPath(ctx);
        CGContextMoveToPoint   (ctx, CGRectGetMinX(rect), CGRectGetMinY(rect));  // top left
        CGContextAddLineToPoint(ctx, CGRectGetMaxX(rect), CGRectGetMidY(rect));  // mid right
        CGContextAddLineToPoint(ctx, CGRectGetMinX(rect), CGRectGetMaxY(rect));  // bottom left
        CGContextClosePath(ctx);
    
        CGContextSetRGBFillColor(ctx, 1, 1, 0, 1);
        CGContextFillPath(ctx);
    }
    

    【讨论】:

    • 您好 :) 我想使用 CGRect 来绘制三角形,而不是添加 UIImageViews。我刚刚相应地编辑了我的问题。 :)
    • 当然,这个自定义视图应该有一个颜色属性,并且该属性将在drawRect:方法中用于绘制三角形。
    • @KeithOYS 嘿 Keith,如果我的解决方案对您有帮助,请点赞/批准。谢谢老兄!
    【解决方案2】:

    将以下方法添加到您的 UiTableviewCell 类中。这是您正在寻找的正确效果:

    -(void)drawRect:(CGRect)rect
    {
        CGContextRef ctx = UIGraphicsGetCurrentContext();
    
        CGContextBeginPath(ctx);
        CGContextMoveToPoint   (ctx, CGRectGetMaxX(rect), CGRectGetMidY(rect));  
        CGContextAddLineToPoint(ctx, CGRectGetMaxX(rect), CGRectGetMinY(rect));  
        CGContextAddLineToPoint(ctx, CGRectGetMaxX(rect)-25, CGRectGetMinY(rect));  
        CGContextClosePath(ctx);
    
        CGContextSetRGBFillColor(ctx, 1, 1, 0, 1);
        CGContextFillPath(ctx);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-15
      • 1970-01-01
      • 1970-01-01
      • 2017-12-15
      • 2014-12-10
      • 1970-01-01
      • 2023-03-17
      相关资源
      最近更新 更多