標籤:
//// YKPopView.m// DrawAnSample//// Created by 高順生 on 15/9/6.// Copyright (c) 2015年 yinyakun. All rights reserved.//#import "YKPopView.h"#define MENUGAP 7@implementation YKPopView-(instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.backgroundColor = [UIColor clearColor]; } return self;}// Only override drawRect: if you perform custom drawing.// An empty implementation adversely affects performance during animation.- (void)drawRect:(CGRect)rect { // Drawing code CGFloat width = rect.size.width; CGFloat height = rect.size.height; CGFloat radius = 3; // 擷取CGContext,注意UIKit裡用的是一個專門的函數 CGContextRef context = UIGraphicsGetCurrentContext(); // 移動到初始點 CGContextMoveToPoint(context, radius, MENUGAP); // 繪製第1條線和第1個1/4圓弧 CGContextAddLineToPoint(context, width * 5/6 - 6,MENUGAP); CGContextAddLineToPoint(context, width * 5/6,0); CGContextAddLineToPoint(context, width * 5/6 + 6,MENUGAP); CGContextAddLineToPoint(context, width - radius, MENUGAP); CGContextAddArc(context, width - radius, radius + MENUGAP, radius, -0.5 * M_PI, 0.0, 0); // 繪製第2條線和第2個1/4圓弧 CGContextAddLineToPoint(context, width, height - radius - MENUGAP); CGContextAddArc(context, width - radius, height - radius - MENUGAP, radius, 0.0, 0.5 * M_PI, 0); // 繪製第3條線和第3個1/4圓弧 CGContextAddLineToPoint(context, radius, height - MENUGAP); CGContextAddArc(context, radius, height - radius - MENUGAP, radius, 0.5 * M_PI, M_PI, 0); // 繪製第4條線和第4個1/4圓弧 CGContextAddLineToPoint(context, 0, radius + MENUGAP); CGContextAddArc(context, radius, radius + MENUGAP, radius, M_PI, 1.5 * M_PI, 0); // 閉合路徑 CGContextClosePath(context); [[UIColor redColor] setFill]; CGContextDrawPath(context, kCGPathFill);}
自訂一個view.
ios 自頂一個view