美文网首页
iOS 为视图添加三角指示图片

iOS 为视图添加三角指示图片

作者: Allence | 来源:发表于2021-05-20 21:19 被阅读0次
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake( 20, 40)];
    [path addLineToPoint:CGPointMake(36, 40)];
    [path addLineToPoint:CGPointMake(28, 50)];
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.fillColor = [UIColor redColor].CGColor;
    layer.path = path.CGPath;
    [self.switchTipLabel.layer addSublayer:layer];

- (void)addView{
    UIImage *triangle = [self triangleImageWithSize:CGSizeMake(16, 10) tintColor:[UIColor redColor]];
    UIImageView *triangleView = [[UIImageView alloc]initWithImage:triangle];
    triangleView.frame = CGRectMake(20, tipHeight, 16, 10);
    [self.switchTipLabel addSubview:triangleView];

}
- (UIImage *)triangleImageWithSize:(CGSize)size tintColor:(UIColor *)tintColor{
    UIGraphicsBeginImageContextWithOptions(size, NO, 0);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(0, 0)];
    [path addLineToPoint:CGPointMake(size.width/2,size.height)];
    [path addLineToPoint:CGPointMake(size.width, 0)];
    [path closePath];
    CGContextSetFillColorWithColor(ctx, tintColor.CGColor);
    [path fill];
    UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return resultImage;
}

相关文章

网友评论

      本文标题:iOS 为视图添加三角指示图片

      本文链接:https://www.haomeiwen.com/subject/ounbjltx.html