iOS 扫描上的层及其样式(二)

要实现上一个图中的扫描区域4个角,可以用以下代码实现

1、新建一个view。

2、将将扫描区域的CGRect传进来。

3、利用

-(void)drawRect:(CGRect)rect方法

4、调整下四个角的所在区域,控制了角的宽和高,


//单个角的两线间距

#define klineDistance 10

//角的宽度

#define cornerWith 30

//角的高度

#define cornerHeight 30



-(void)drawRect:(CGRect)rect

{

    [selfleftUpCorner];

    [selfrightUpCorner];

    [selfrightBottonCorner];

    [selfleftBottonCorner];

}


#pragma mark -左上角

-(void)leftUpCorner

{

    UIBezierPath *path = [UIBezierPath bezierPath];

    

    CGFloat x = 2;

    CGFloat y = 2;

    

    [path moveToPoint:CGPointMake(x, y)]; //起始点

    [path addLineToPoint:CGPointMake(cornerWith + x, y)];

    [path addLineToPoint:CGPointMake(cornerWith + x, y + klineDistance)];

    [path addLineToPoint:CGPointMake(klineDistance + x, y + klineDistance)];

    [path addLineToPoint:CGPointMake(klineDistance + x,cornerHeight + y)];

    [path addLineToPoint:CGPointMake(x, cornerHeight + y)];

    [path closePath];

    

    //获取当前环境

    CGContextRef context = UIGraphicsGetCurrentContext();

    //保存当前环境,便于以后恢复

    CGContextSaveGState(context);

    

    //将坐标的起点

    CGContextTranslateCTM(context, 0, 0);

    //将当前的颜色变成黄色

    UIColor* fillColor = [UIColor redColor];

    [fillColor setFill];

    //五角星填充为黄色

    [path fill];

    

    CGContextRestoreGState(context);

}


#pragma mark -右上角

-(void)rightUpCorner

{

    UIBezierPath *path = [UIBezierPath bezierPath];

    

    CGSize size = self.rect.size;

    

    CGFloat x = size.width - cornerWith - 2;

    CGFloat y = 2;

    

    [path moveToPoint:CGPointMake(x, y)]; //起始点

    [path addLineToPoint:CGPointMake(x + cornerWith, y)];

    [path addLineToPoint:CGPointMake(x + cornerWith , y + cornerHeight)];

    [path addLineToPoint:CGPointMake(x + cornerWith - klineDistance , y + cornerHeight)];

    [path addLineToPoint:CGPointMake(x + cornerWith - klineDistance , y + klineDistance)];

    [path addLineToPoint:CGPointMake(x , y + klineDistance)];

    [path closePath];

    

    //获取当前环境

    CGContextRef context = UIGraphicsGetCurrentContext();

    //保存当前环境,便于以后恢复

    CGContextSaveGState(context);

    

    //将坐标的起点变成(100100

    CGContextTranslateCTM(context, 0, 0);

    //将当前的颜色变成黄色

    UIColor* fillColor = [UIColor redColor];

    [fillColor setFill];

    //五角星填充为黄色

    [path fill];

    

    CGContextRestoreGState(context);

}


#pragma mark -右下角

-(void)rightBottonCorner

{

    UIBezierPath *path = [UIBezierPath bezierPath];

    

    CGSize size = self.rect.size;

    

    CGFloat x = size.width - cornerWith - 2;

    CGFloat y = size.height - cornerHeight - 2;

    

    [path moveToPoint:CGPointMake(x + cornerWith - klineDistance, y)]; //起始点

    [path addLineToPoint:CGPointMake(x + cornerWith , y)];

    [path addLineToPoint:CGPointMake(x + cornerWith , y + cornerHeight)];

    [path addLineToPoint:CGPointMake(x , y + cornerHeight)];

    [path addLineToPoint:CGPointMake(x , y + cornerHeight - klineDistance)];

    [path addLineToPoint:CGPointMake(x + cornerWith - klineDistance , y + cornerHeight - klineDistance)];

    [path closePath];

    

    //获取当前环境

    CGContextRef context = UIGraphicsGetCurrentContext();

    //保存当前环境,便于以后恢复

    CGContextSaveGState(context);

    

    CGContextTranslateCTM(context, 0, 0);

    //将当前的颜色变成黄色

    UIColor* fillColor = [UIColor redColor];

    [fillColor setFill];

    //五角星填充为黄色

    [path fill];

    

    CGContextRestoreGState(context);

}


#pragma mark -左下角

-(void)leftBottonCorner

{

    UIBezierPath *path = [UIBezierPath bezierPath];

    

    CGSize size = self.rect.size;

    

    CGFloat x = 2;

    CGFloat y = size.height - cornerHeight - 2;

    

    [path moveToPoint:CGPointMake(x , y)]; //起始点

    [path addLineToPoint:CGPointMake(x + klineDistance , y)];

    [path addLineToPoint:CGPointMake(x + klineDistance , y + cornerHeight - klineDistance)];

    [path addLineToPoint:CGPointMake(x + cornerWith , y + cornerHeight - klineDistance)];

    [path addLineToPoint:CGPointMake(x + cornerWith , y + cornerHeight)];

    [path addLineToPoint:CGPointMake(x , y + cornerHeight)];

    [path closePath];

    

    //获取当前环境

    CGContextRef context = UIGraphicsGetCurrentContext();

    //保存当前环境,便于以后恢复

    CGContextSaveGState(context);

    

    //将坐标的起点变成(100100

    CGContextTranslateCTM(context, 0, 0);

    //将当前的颜色变成黄色

    UIColor* fillColor = [UIColor redColor];

    [fillColor setFill];

    //五角星填充为黄色

    [path fill];

    

    CGContextRestoreGState(context);

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值