绘制三角形 相对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

{
// 设置背景色
[[UIColor whiteColor] set];
//拿到当前视图准备好的画板

CGContextRef  context = UIGraphicsGetCurrentContext();

//利用path进行绘制三角形

CGContextBeginPath(context);//标记
CGFloat location = self.frame.size.width - 30 - 10 - 10;
CGContextMoveToPoint(context,
                     location+8, 50);//设置起点 上点

CGContextAddLineToPoint(context,
                        location , 60);//左侧

CGContextAddLineToPoint(context,
                        location + 16, 60);//右侧

CGContextClosePath(context);//路径结束标志,不写默认封闭

[[UIColor whiteColor] setFill];  //设置填充色

[[UIColor whiteColor] setStroke]; //设置边框颜色

CGContextDrawPath(context,
                  kCGPathFillStroke);//绘制路径path

}

@end

你可能感兴趣的:(绘制三角形 相对view)