iOS 扫描上的层及其样式(二)
2015-08-18 16:57
751 查看
要实现上一个图中的扫描区域4个角,可以用以下代码实现
1、新建一个view。
2、将将扫描区域的CGRect传进来。
3、利用
-(void)drawRect:(CGRect)rect方法
4、调整下四个角的所在区域,控制了角的宽和高,
//单个角的两线间距
#define klineDistance 10
//角的宽度
#define cornerWith 30
<
4000
p style="margin-top:0px;margin-bottom:0px;font-size:11px;font-family:'Heiti SC Light';color:rgb(0,132,0);">
//角的高度
#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);
//将坐标的起点变成(100,100)
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);
//将坐标的起点变成(100,100)
CGContextTranslateCTM(context,
0, 0);
//将当前的颜色变成黄色
UIColor* fillColor = [UIColor
redColor];
[fillColor
setFill];
//五角星填充为黄色
[path
fill];
CGContextRestoreGState(context);
}
1、新建一个view。
2、将将扫描区域的CGRect传进来。
3、利用
-(void)drawRect:(CGRect)rect方法
4、调整下四个角的所在区域,控制了角的宽和高,
//单个角的两线间距
#define klineDistance 10
//角的宽度
#define cornerWith 30
<
4000
p style="margin-top:0px;margin-bottom:0px;font-size:11px;font-family:'Heiti SC Light';color:rgb(0,132,0);">
//角的高度
#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);
//将坐标的起点变成(100,100)
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);
//将坐标的起点变成(100,100)
CGContextTranslateCTM(context,
0, 0);
//将当前的颜色变成黄色
UIColor* fillColor = [UIColor
redColor];
[fillColor
setFill];
//五角星填充为黄色
[path
fill];
CGContextRestoreGState(context);
}
相关文章推荐
- CALayer与UIView的关系
- UIView的bounds和frame区别
- uiview
- uiview生命周期
- UIView翻译 (参考)
- iOS 转变UIView到UIImage
- UIView动画
- initWithFrame 和 initWithCoder
- [CS193P] 第六堂課摘要及心得筆記
- UI第一天
- 棋盘
- ios实现button变换颜色并可以放大、缩小、旋转
- uiview contentViewmode
- iphone开发小记 基础知识2
- iphone开发小记 基础知识3
- [笔记]I 群聊天记录2012/03/中
- iPhone开发进阶(2) --- iPhone应用程序/项目的构成
- 视图弹出后放大又缩小的动画实现、类似于alertView效果
- iPhone SDK开发:自定义UIAlertView
- IOS开发中的UIScreen、UIView、UIWindow和UIViewController