学习context后自定义虚线圆角按钮,举一反三,其他控件也可修改

.h文件


#import <UIKit/UIKit.h>


@interface dashRoundCornerBtn : UIButton


@property(nonatomic ,retain)UIColor *lineColor;///<线的颜色 缺省:蓝色

@property(nonatomic ,assign)float lineWidth;///<线宽 缺省:1

@property(nonatomic ,assign)BOOL isDash;//是否为虚线 缺省:否

@property(nonatomic ,assign)float radius;///<圆角半径 缺省:10


@end







.m文件

#import "dashRoundCornerBtn.h"

@interface dashRoundCornerBtn ()

@property(nonatomic ,retain)UIColor *backColor;

@end

@implementation dashRoundCornerBtn

//懒加载

-(UIColor *)lineColor

{

    if (!_lineColor)

    {

        self.lineColor = [UIColor blueColor];

    }

    return _lineColor;

}


-(float)lineWidth

{

    if (!_lineWidth)

    {

        self.lineWidth = 1;

    }

    return _lineWidth;

}


-(BOOL)isDash

{

    if (!_isDash)

    {

        self.isDash = NO;

    }

    return _isDash;

}


-(float)radius

{

    if (!_radius)

    {

        self.radius = 10;

    }

    return _radius;

}


-(UIColor *)backgroundColor

{

    if (!_backColor)

    {

        self.backColor = [UIColor clearColor];

    }

    return _backColor;

}


-(void)setBackgroundColor:(UIColor *)backgroundColor

{

    _backColor = backgroundColor;

}


// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect

{

    float width = rect.size.width;

    float height = rect.size.height;

    float lineWidth = self.lineWidth;

    float radius = self.radius;

    CGContextRef context = UIGraphicsGetCurrentContext();

    //设置线条样式

    CGContextSetLineCap(context, kCGLineCapSquare);

    //设置线条粗细宽度

    CGContextSetLineWidth(context, lineWidth);

    if (self.isDash)

    {

        CGFloat dashArray[] = {5*lineWidth};

        CGContextSetLineDash(context, 0, dashArray, 1);//画虚线

    }

    CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);

    CGContextMoveToPoint(context, (lineWidth+radius), lineWidth);

    CGContextAddLineToPoint(context, width-(lineWidth+radius), lineWidth);

    CGContextAddArc(context, width-(lineWidth+radius), (lineWidth+radius), radius, -M_PI_2, 0, 0);

    CGContextAddLineToPoint(context, width-lineWidth, height-(lineWidth+radius));

    CGContextAddArc(context, width-(lineWidth+radius), height-(lineWidth+radius), radius, 0, M_PI_2, 0);

    CGContextAddLineToPoint(context, (lineWidth+radius), height-lineWidth);

    CGContextAddArc(context, (lineWidth+radius), height-(lineWidth+radius), radius, M_PI_2, M_PI, 0);

    CGContextAddLineToPoint(context, lineWidth, (lineWidth+radius));

    CGContextAddArc(context, (lineWidth+radius), (lineWidth+radius),radius, M_PI, -M_PI_2, 0);

    CGContextStrokePath(context);


    //填充了一段路径:

    CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);

    CGContextMoveToPoint(context, (lineWidth+radius), lineWidth);

    CGContextAddLineToPoint(context, width-(lineWidth+radius), lineWidth);

    CGContextAddArc(context, width-(lineWidth+radius), (lineWidth+radius), radius, -M_PI_2, 0, 0);

    CGContextAddLineToPoint(context, width-lineWidth, height-(lineWidth+radius));

    CGContextAddArc(context, width-(lineWidth+radius), height-(lineWidth+radius), radius, 0, M_PI_2, 0);

    CGContextAddLineToPoint(context, (lineWidth+radius), height-lineWidth);

    CGContextAddArc(context, (lineWidth+radius), height-(lineWidth+radius), radius, M_PI_2, M_PI, 0);

    CGContextAddLineToPoint(context, lineWidth, (lineWidth+radius));

    CGContextAddArc(context, (lineWidth+radius), (lineWidth+radius),radius, M_PI, -M_PI_2, 0);

    CGContextSetFillColorWithColor(context, self.backColor.CGColor);

    CGContextFillPath(context);

}

@end




调用

#import "ViewController.h"

#import "dashRoundCornerBtn.h"


@interface ViewController ()


@property (weak, nonatomic) IBOutlet dashRoundCornerBtn *btn;

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    self.btn.isDash = YES;

    self.btn.lineWidth = 2;

    self.btn.radius = 20;

    // Do any additional setup after loading the view, typically from a nib.

}

@end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值