转自:http://marshal.easymorse.com/archives/4046

 

如何在iOS屏幕上画出一条线来?这是一切复杂画线的基础。之前在计算一个点是否在一个区域中中已经有使用CGPath的示例。那是用来标定区域的,这里使用CGPath画线。

【转】最简单的画线功能

示例非常简单。

首先要有个UIImageView,在本例中声明为成员变量:

@interface PathDemoViewController : UIViewController { 
    UIImageView *imageView; 
}

画线的代码:

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    
    imageView=[[UIImageView alloc] initWithFrame:self.view.frame]; 
    [self.view addSubview:imageView]; 
    
    self.view.backgroundColor=[UIColor blueColor]; 
    
    UIGraphicsBeginImageContext(imageView.frame.size); 
    [imageView.image drawInRect:CGRectMake(0, 0, imageView.frame.size.width, imageView.frame.size.height)]; 
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 15.0); 
    CGContextSetAllowsAntialiasing(UIGraphicsGetCurrentContext(), YES); 
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 1.0, 0.0, 0.0, 1.0); 
    CGContextBeginPath(UIGraphicsGetCurrentContext()); 
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), 100, 100); 
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), 200, 100); 
    CGContextStrokePath(UIGraphicsGetCurrentContext()); 
    imageView.image=UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
}

其中:

CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);

设置了线的边缘样式:

【转】最简单的画线功能 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-06-21
  • 2022-12-23
  • 2021-11-04
  • 2021-10-21
  • 2021-11-02
  • 2022-01-04
猜你喜欢
  • 2022-12-23
  • 2021-12-12
  • 2021-08-17
  • 2022-12-23
  • 2021-11-23
  • 2022-12-23
  • 2021-03-31
相关资源
相似解决方案