繪制三角形 相對view

//
// san.m
// san
//
// Created by baipeng on 2017/6/12.
// Copyright ? 2017年 BPG. All rights reserved.
//

import "san.h"

@implementation san

  • (instancetype)initWithFrame:(CGRect)frame
    {
    self = [super initWithFrame:frame];
    if (self) {
    UIView *bac = [[UIView alloc]initWithFrame:[self menuFrame]];
    bac.backgroundColor = [UIColor whiteColor];
    bac.layer.masksToBounds = YES;
    bac.layer.cornerRadius = 4.0;
    [self addSubview:bac];
    }
    return self;
    }
  • (CGRect)menuFrame {
    CGFloat menuX = [UIScreen mainScreen].bounds.size.width - 180;
    CGFloat menuY = 60;
    CGFloat width = 150;
    CGFloat heigh = 40 * 6;
    return (CGRect){menuX,menuY,width,heigh};
    }

pragma mark 繪制三角形

  • (void)drawRect:(CGRect)rect

{
// 設(shè)置背景色
[[UIColor whiteColor] set];
//拿到當(dāng)前視圖準(zhǔn)備好的畫板

CGContextRef  context = UIGraphicsGetCurrentContext();

//利用path進(jìn)行繪制三角形

CGContextBeginPath(context);//標(biāo)記
CGFloat location = self.frame.size.width - 30 - 10 - 10;
CGContextMoveToPoint(context,
                     location+8, 50);//設(shè)置起點 上點

CGContextAddLineToPoint(context,
                        location , 60);//左側(cè)

CGContextAddLineToPoint(context,
                        location + 16, 60);//右側(cè)

CGContextClosePath(context);//路徑結(jié)束標(biāo)志,不寫默認(rèn)封閉

[[UIColor whiteColor] setFill];  //設(shè)置填充色

[[UIColor whiteColor] setStroke]; //設(shè)置邊框顏色

CGContextDrawPath(context,
                  kCGPathFillStroke);//繪制路徑path

}

@end

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容