本文共 3604 字,大约阅读时间需要 12 分钟。
UIBezierPath的使用
1. 使用UIBezierPath绘制多边形
// 获取path UIBezierPath *aPath = [UIBezierPath bezierPath]; // 设定起始点 [aPath moveToPoint:CGPointMake(0.0f, 0.0f)]; // 添加点 [aPath addLineToPoint:CGPointMake(100.0f, 100.0f)]; [aPath addLineToPoint:CGPointMake(0.f, 50.f)]; // 闭合path [aPath closePath];
2. 使用UIBezierPath绘制圆形
// 将常数转换为度数 #define DEGREES(degrees) ((3.14159265359f * degrees)/ 180.f) // 获取path UIBezierPath *aPath = \ [UIBezierPath bezierPathWithArcCenter:CGPointMake(50, 50) // 圆的中心 radius:50.f // 圆的半径 startAngle:DEGREES(0) // 起始点 endAngle:DEGREES(360) // 结束点 clockwise:YES]; // 顺时针
3. 使用UIBezierPath绘制矩形
// 获取path UIBezierPath *aPath = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 100, 100)];
4. 使用UIBezierPath绘制椭圆
// 获取path UIBezierPath *aPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 50, 70)];
5. 使用UIBezierPath绘制圆角矩形
// 获取path UIBezierPath *aPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 100, 100) cornerRadius:10.f];
6. 使用UIBezierPath绘制带部分圆角的矩形
// 获取path UIBezierPath *aPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 100, 100) byRoundingCorners:UIRectCornerTopLeft cornerRadii:CGSizeMake(100, 100)];
7. 使用UIBezierPath绘制不规则曲线1
// 获取path UIBezierPath *aPath = [UIBezierPath bezierPath]; // 设定起始点 [aPath moveToPoint:CGPointMake(0.0f, 0.0f)]; // 添加一个不规则点 [aPath addCurveToPoint:CGPointMake(100.f, 100.f) controlPoint1:CGPointMake(50.f, 0.f) // 开始点 controlPoint2:CGPointMake(0.f, 50.f)]; // 结束点 // 添加一个点 [aPath addLineToPoint:CGPointMake(0.0f, 100.f)]; // 闭合path [aPath closePath];
8. 使用UIBezierPath绘制不规则曲线2
// 获取path UIBezierPath *aPath = [UIBezierPath bezierPath]; // 设定起始点 [aPath moveToPoint:CGPointMake(0.0f, 0.0f)]; // 添加一个不规则点 [aPath addQuadCurveToPoint:CGPointMake(100.f, 100.f) controlPoint:CGPointMake(0.f, 90.f)]; // 控制点 // 添加一个点 [aPath addLineToPoint:CGPointMake(0.0f, 100.f)]; // 闭合path [aPath closePath];
9. 使用path与CAShapeLayer配合制作mask遮罩效果(path闭环里面的填充区域就是作为遮罩使用的)
// 创建一个view UIView *showView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)]; showView.backgroundColor = [UIColor greenColor]; showView.layer.contents = (__bridge id)([UIImage imageNamed:@"1"].CGImage); // 创建一个椭圆的path UIBezierPath *aPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 100, 100)]; // 创建一个CAShapeLayer并获取椭圆的path CAShapeLayer *layer = [CAShapeLayer layer]; layer.path = aPath.CGPath; // 把这个CAShapeLayer添加为mask showView.layer.mask = layer;
UIBezierPath
calls
转载地址:http://axbno.baihongyu.com/