iOS 创建一个小三角指示器

效果

直接用initFrame方法创建

[[TriangleView alloc] initWithFrame:CGRectMake(0,  - 8, 16, 8)];

.m文件只重写一个drawRect方法就OK

@implementation TriangleView

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextMoveToPoint(context, self.frame.size.width / 2.0, 0);
    
    CGContextAddLineToPoint(context, 0, self.frame.size.height);
    
    CGContextAddLineToPoint(context, self.frame.size.width, self.frame.size.height);
    
    CGContextClosePath(context);
    
    [[UIColor clearColor] setStroke];
    
    [[UIColor whiteColor] setFill];
    
    CGContextDrawPath(context, kCGPathFillStroke);
}

@end

你可能感兴趣的:(iOS 创建一个小三角指示器)