【发布时间】:2023-03-19 06:30:01
【问题描述】:
我想使用 CGRect 而不是 UIImageView 绘制一个三角形,并将其作为子视图添加到某些特定表格单元格的右上角,类似于它在 WWDC 应用程序上的完成方式。
欢迎任何建议。 :)
【问题讨论】:
标签: ios iphone objective-c tableview subview
我想使用 CGRect 而不是 UIImageView 绘制一个三角形,并将其作为子视图添加到某些特定表格单元格的右上角,类似于它在 WWDC 应用程序上的完成方式。
欢迎任何建议。 :)
【问题讨论】:
标签: ios iphone objective-c tableview subview
一种简单的方法是让不同的 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);
}
【讨论】:
drawRect:方法中用于绘制三角形。
将以下方法添加到您的 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);
}
【讨论】: