UIBezierPath shapelayer

 


/* 创建和使用一个path对象的过程是分开的。创建path是第一步,包含一下步骤: 1)创建一个Bezier path对象。 2)使用方法moveToPoint:去设置初始线段的起点。 3)添加line或者curve去定义一个或者多个subpaths 4)改变UIBezierPath对象跟绘图相关的属性。  方法moveToPoint:设置我们想要创建形状的起点。从这点开始,我们可以用方法addLineToPoint:去创建一个形状的线段。  */
#import "ViewController.h"


@interface ViewController ()
@end

@implementation ViewController




- (void)viewDidLoad {
    [super viewDidLoad];
   
    //画五角
    [self drawPentagon];
   
    //画曲线
    [self drawRect:self.view.bounds];
  
}
- (void)drawPentagon{
   
    //(1)UIBezierPath对象
   
    UIBezierPath *aPath = [UIBezierPath bezierPath];
   
    //开始点
   
    [aPath moveToPoint:CGPointMake(100.0, 1.0)];
   
    //划线点
   

   
    [aPath addLineToPoint:CGPointMake(200.0, 40.0)];
   
    [aPath addLineToPoint:CGPointMake(160, 140)];
   
    [aPath addLineToPoint:CGPointMake(40.0, 140)];
   
    [aPath addLineToPoint:CGPointMake(0.0, 40.0)];
   
    [aPath closePath];//关闭路径(封闭)
   
    //设置定点是个5*5的小圆形(自己加的)
   
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(100-5/2.0, 0, 5, 5)];
   
    [aPath appendPath:path];
   
    //2)加到CAShapeLayer,效果出来了
    //shape:模型
    CAShapeLayer *shapelayer = [CAShapeLayer layer];
   
    //设置边框颜色
   
    shapelayer.strokeColor = [[UIColor cyanColor]CGColor];
   
    //设置填充颜色
   
    shapelayer.fillColor = [[UIColor redColor]CGColor];       //***就是这句话在关联彼此(UIBezierPath和CAShapeLayer):       shapelayer.path = aPath.CGPath;           [self.view.layer addSublayer:shapelayer];   }
  //***就是这句话在关联彼此(UIBezierPathCAShapeLayer):
   
    shapelayer.path = aPath.CGPath;
   
   
 
    [self.view.layer addSublayer:shapelayer];
   
}

- (void)drawRect:(CGRect)rect
{
    UIColor *color = [UIColor yellowColor];
    [color set]; //设置线条颜色
   
    UIBezierPath* aPath = [UIBezierPath bezierPath];
   
    aPath.lineWidth = 5.0;
    aPath.lineCapStyle = kCGLineCapRound; //线条拐角
    aPath.lineJoinStyle = kCGLineCapRound; //终点处理
   
    [aPath moveToPoint:CGPointMake(20, 50)];
   
    //CurveToPoint:终点
    [aPath addCurveToPoint:CGPointMake(200, 50) controlPoint1:CGPointMake(110, 0) controlPoint2:CGPointMake(110, 100) ];
   
  
   
  
   
    [aPath stroke];
   
    CAShapeLayer *shapelayer = [CAShapeLayer layer];
    shapelayer.strokeColor = [[UIColor redColor]CGColor];
    shapelayer.fillColor = [[UIColor whiteColor]CGColor];
    shapelayer.path = aPath.CGPath;
    [self.view.layer addSublayer:shapelayer];
   
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
 
 
 
posted @ 2015-12-10 19:53  大大的太阳ing  阅读(355)  评论(0)    收藏  举报