首页 > 代码库 > 画直线
画直线
方法一(推荐):使用CALayer
CALayer *middleBorder = [CALayer layer];middleBorder.frame = CGRectMake(x, y, width, height);middleBorder.backgroundColor = UIColor.CGColor;[myView.layer addSublayer:middleBorder];
方法二:使用UIImageView(不便于更改)
1 - (void)drawLineWithPoint:(CGPoint) startPoint toPoint:(CGPoint)toPoint 2 { 3 CGSize screenSize = [[UIScreen mainScreen] applicationFrame].size; 4 5 UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, screenSize.width, screenSize.height)]; 6 // UIImageView *imageView = [[UIImageView alloc] init]; 7 // imageView.frame = self.contentView.frame; 8 [self.contentView addSubview:imageView]; 9 10 UIGraphicsBeginImageContext(imageView.frame.size);11 [imageView.image drawInRect:CGRectMake(0, 0, imageView.frame.size.width, imageView.frame.size.height)];12 13 //获得处理的上下文14 CGContextRef context = UIGraphicsGetCurrentContext();15 16 //指定直线样式17 CGContextSetLineCap(context, kCGLineCapSquare);18 19 //直线宽度20 CGContextSetLineWidth(context, 1.0);21 22 //设置颜色23 // red:166/255.0 green:177/255.0 blue:186/255.024 CGContextSetRGBStrokeColor(context, 246.0/255.0, 247.0/255.0, 247.0/255.0, 1.0);25 26 //开始绘制27 CGContextBeginPath(context);28 29 //画笔移动到点(31,170)30 CGContextMoveToPoint(context, startPoint.x, startPoint.y);31 32 //下一点33 CGContextAddLineToPoint(context, toPoint.x, toPoint.y);34 35 //绘制完成36 CGContextStrokePath(context);37 38 imageView.image = UIGraphicsGetImageFromCurrentImageContext();39 UIGraphicsEndImageContext();40 41 // NSLog(@"%f, %f", imageView.frame.size.width, imageView.frame.size.height);42 }
画直线
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。