UIBezierPath和CAShapeLayer

CGPoint point1 = CGPointMake(100, 100);
CGPoint point2 = CGPointMake(100, 300);

//画直线
UIBezierPath *path =  [[UIBezierPath alloc] init];
[path moveToPoint:point1];
[path addLineToPoint:point2];

//画曲线
//controlPoint圆弧的外切三角形的顶点
CGPoint controlPoint = CGPointMake(CGRectGetWidth(self.view.frame), point1.y + (point2.y-point1.y)/2);
[path moveToPoint:point1];
[path addQuadCurveToPoint:point2 controlPoint:controlPoint];




CGPoint point_1 = CGPointMake(0, point1.y);
CGPoint point_2 = CGPointMake(CGRectGetWidth(self.view.bounds), point1.y);
CGPoint point_3 = CGPointMake(0, point2.y);
CGPoint point_4 = CGPointMake(CGRectGetWidth(self.view.bounds), point2.y);

[path moveToPoint:point_1];
[path addLineToPoint:point_2];
[path addLineToPoint:point_4];
[path addLineToPoint:point_3];

CAShapeLayer *shapeLayer3 = [CAShapeLayer layer];
shapeLayer3.strokeColor = [UIColor redColor].CGColor;
shapeLayer3.fillColor = [UIColor cyanColor].CGColor;
shapeLayer3.lineWidth = 4;
shapeLayer3.path = path.CGPath;
//这个属性设置的效果是几个闭合空间的交集颜色会抵消,但是它的设置有效的前提是几个闭合空间公用一个path
shapeLayer3.fillRule = kCAFillRuleEvenOdd;
[self.view.layer addSublayer:shapeLayer3];

还可以利用CAShapeLayer的下面两个属性做出各种动画(微信之前的下拉出现一个眼睛就是用的这个)

@property CGFloat strokeStart;
@property CGFloat strokeEnd;

你可能感兴趣的:(UIBezierPath和CAShapeLayer)