1.空心椭圆
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(110, 100, 150, 100) cornerRadius:50];
CAShapeLayer *layer = [[CAShapeLayer alloc] init];
layer.path = path.CGPath;
layer.fillColor = UIColor.clearColor.CGColor; // 空心
layer.strokeColor = UIColor.blackColor.CGColor; // 黑边
//layer.lineWidth = 5; // 边宽度
[self.view.layer addSublayer:layer];
2.空心圆
CGFloat radius = 60; // 半径
CGFloat startAngle = 0; // 起始角度
CGFloat endAngle = (M_PI * 2); // 结束角度
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:self.view.center radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES];
CAShapeLayer *layer = [[CAShapeLayer alloc] init];
layer.path = path.CGPath;
layer.fillColor = UIColor.clearColor.CGColor;
layer.strokeColor = UIColor.blackColor.CGColor;
[self.view.layer addSublayer:layer];
3.曲线
- 用
path.moveToPoint(startPoint)
让它移动到起始点
- 用
path.addQuadCurveToPoint(endPoint, controlPoint: controlPoint)
这个方法告诉它结束点和控制点
- 两个控制点可以使用方法
path.addCurveToPoint(endPoint, controlPoint1: controlPoint, controlPoint2: controlPoint2)
来搞定
CGPoint startPoint = CGPointMake(50, 300);
CGPoint endPoint = CGPointMake(280, 300);
CGPoint controlPoint = CGPointMake(170, 200);
// 红色方块
CALayer *layer1 = [[CALayer alloc] init];
layer1.frame = CGRectMake(startPoint.x, startPoint.y, 5, 5);
layer1.backgroundColor = UIColor.redColor.CGColor;
// 红色方块
CALayer *layer2 = [[CALayer alloc] init];
layer2.frame = CGRectMake(endPoint.x, endPoint.y, 5, 5);
layer2.backgroundColor = UIColor.redColor.CGColor;
// 红色方块
CALayer *layer3 = [[CALayer alloc] init];
layer3.frame = CGRectMake(controlPoint.x, controlPoint.y, 5, 5);
layer3.backgroundColor = UIColor.redColor.CGColor;
UIBezierPath *path = [[UIBezierPath alloc] init];
CAShapeLayer *layer = [[CAShapeLayer alloc] init];
[path moveToPoint:startPoint];
[path addQuadCurveToPoint:endPoint controlPoint:controlPoint];
layer.path = path.CGPath;
layer.fillColor = UIColor.clearColor.CGColor;
layer.strokeColor = UIColor.blackColor.CGColor;
[self.view.layer addSublayer:layer];
[self.view.layer addSublayer:layer1];
[self.view.layer addSublayer:layer2];
[self.view.layer addSublayer:layer3];
4.其他形状
CGFloat left = 20;
CGSize finalSize = CGSizeMake(self.view.frame.size.width - left, 400);
CGFloat layerHeight = finalSize.height * 0.2;
CAShapeLayer *layer = [[CAShapeLayer alloc] init];
UIBezierPath *bezier = [[UIBezierPath alloc] init];
// 起始点
[bezier moveToPoint:CGPointMake(left, finalSize.height - layerHeight)];
[bezier addLineToPoint:CGPointMake(left, finalSize.height - 1)];
[bezier addLineToPoint:CGPointMake(finalSize.width, finalSize.height - 1)];
[bezier addLineToPoint:CGPointMake(finalSize.width, finalSize.height - layerHeight)];
// 突出点
[bezier addQuadCurveToPoint:CGPointMake(left,finalSize.height - layerHeight) controlPoint:CGPointMake(self.view.frame.size.width / 2, (finalSize.height - layerHeight) - 60)];
layer.path = bezier.CGPath;
layer.fillColor = UIColor.clearColor.CGColor;
layer.strokeColor = UIColor.greenColor.CGColor;
[self.view.layer addSublayer:layer];