簡體   English   中英

具有自定義形狀的UIView

[英]UIView with custom shape

我想要一個具有自定義形狀的UIElement(UIView,UIButton,UILabel等中的任何一個),可以說一個三角形。 您如何建議實現它。
提前致謝。

這是一個UIView子類(三角形)的示例:

@implementation TriangleView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)drawRect:(CGRect)rect
{
    // Drawing code
    CGContextRef ctx = UIGraphicsGetCurrentContext();

    CGContextBeginPath(ctx);

    CGContextMoveToPoint   (ctx, CGRectGetMinX(rect), CGRectGetMaxY(rect));  // top left
    CGContextAddLineToPoint(ctx, CGRectGetMidX(rect), CGRectGetMinY(rect));  // mid right
    CGContextAddLineToPoint(ctx, CGRectGetMaxX(rect), CGRectGetMaxY(rect));  // bottom left

    CGContextClosePath(ctx);

    CGContextSetRGBFillColor(ctx, 241/255.0, 241/255.0, 241/255.0, 1);
    CGContextFillPath(ctx);

}

@end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM