UIBezierPath基本用法及画圆角

本文介绍如何使用UIBezierPath实现复杂的圆角效果,包括画圆角的基本方法及通过添加路径来绘制特殊形状的技巧。
部署运行你感兴趣的模型镜像

首先说一下画圆角吧,其实UIBezierPath的画角规则是这样的:这里写图片描述
代码:

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong) CAShapeLayer *shapeLayer;

@property (nonatomic, strong) UIBezierPath      *path;

@property (nonatomic, strong) UIView *maskView;

@end

@implementation ViewController

- (CAShapeLayer *)shapeLayer
{
    if (!_shapeLayer) {

        _shapeLayer = [[CAShapeLayer alloc] init];
        _shapeLayer.path = self.appendPath.CGPath;
        _shapeLayer.fillColor = [UIColor redColor].CGColor;
//        _shapeLayer.strokeColor = [UIColor greenColor].CGColor;
//        _shapeLayer.lineWidth = 1.0;
        _shapeLayer.fillRule = kCAFillRuleEvenOdd;

    }
    return _shapeLayer;
}

//- (UIBezierPath *)path
//{
//    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, CGRectGetWidth(self.view.frame) * 0.8, 35) cornerRadius:5];
//
//    [bezierPath appendPath:self.appendPath];
//    [bezierPath setUsesEvenOddFillRule:YES];
//    return bezierPath;
//}

#define DEGREES_TO_RADIANS(degrees) ((3.14159265359 * degrees) / 180)

- (UIBezierPath *)appendPath
{
    UIBezierPath *appendPath = [UIBezierPath bezierPath];

    [appendPath moveToPoint:CGPointMake(CGRectGetWidth(_maskView.frame) - 3, 0)];

    [appendPath addLineToPoint:CGPointMake(CGRectGetWidth(_maskView.frame) - 20, 10)];

    [appendPath addLineToPoint:CGPointMake(0, 10)];

    [appendPath addArcWithCenter:CGPointMake(5, 15) radius:5 startAngle:DEGREES_TO_RADIANS(270) endAngle:DEGREES_TO_RADIANS(180) clockwise:NO];
    // 添加圆到path
//    [appendPath addArcWithCenter:CGPointMake(5, 15) radius:5.0 startAngle:DEGREES_TO_RADIANS(90) endAngle:DEGREES_TO_RADIANS(90) clockwise:YES];

    [appendPath addLineToPoint:CGPointMake(0, CGRectGetHeight(_maskView.frame))];

    [appendPath addLineToPoint:CGPointMake(CGRectGetWidth(_maskView.frame), CGRectGetHeight(_maskView.frame))];

    [appendPath addLineToPoint:CGPointMake(CGRectGetWidth(_maskView.frame), 10)];

    [appendPath addLineToPoint:CGPointMake(CGRectGetWidth(_maskView.frame) - 3, 0)];

    return appendPath;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];


    _maskView = [[UIView alloc] initWithFrame:CGRectMake(20, 200, CGRectGetWidth(self.view.frame) * 0.8, 60)];
    _maskView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_maskView];

    [_maskView.layer addSublayer:self.shapeLayer];
}

其中

- (void)addArcWithCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise

其参数意义:

center:圆心的坐标

radius:半径

startAngle:起始的弧度

endAngle:圆弧结束的弧度

clockwise:YES为顺时针,No为逆时针

初始化方法

+ (instancetype)bezierPath;

//创建一个矩形
+ (instancetype)bezierPathWithRect:(CGRect)rect;

//创建圆形或者椭圆形
+ (instancetype)bezierPathWithOvalInRect:(CGRect)rect;

//根据矩形画带圆角的曲线
+ (instancetype)bezierPathWithRoundedRect:(CGRect)rect cornerRadius:(CGFloat)cornerRadius; // rounds all corners with the same horizontal and vertical radius

//在矩形中,可以针对四角中的某个角加圆角
+ (instancetype)bezierPathWithRoundedRect:(CGRect)rect byRoundingCorners:(UIRectCorner)corners cornerRadii:(CGSize)cornerRadii; 
/**
参数:
corners:枚举值,可以选择某个角

cornerRadii:圆角的大小
*/

//以某个中心点画弧线
+ (instancetype)bezierPathWithArcCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise;
/**
参数:
center:弧线中心点的坐标

radius:弧线所在圆的半径

startAngle:弧线开始的角度值

endAngle:弧线结束的角度值

clockwise:是否顺时针画弧线
*/

// 根据CGPath创建并返回一个新的UIBezierPath对象
+ (instancetype)bezierPathWithCGPath:(CGPathRef)CGPath;

//画二元曲线,一般和moveToPoint配合使用
- (void)addQuadCurveToPoint:(CGPoint)endPoint controlPoint:(CGPoint)controlPoint
/**
参数:
endPoint:曲线的终点

controlPoint:画曲线的基准点
*/

//以三个点画一段曲线,一般和moveToPoint配合使用
- (void)addCurveToPoint:(CGPoint)endPoint controlPoint1:(CGPoint)controlPoint1 controlPoint2:(CGPoint)controlPoint2
/**
参数:

endPoint:曲线的终点

controlPoint1:画曲线的第一个基准点

controlPoint2:画曲线的第二个基准点
*/

基本用法:

//设置描绘的起点
- (void)moveToPoint:(CGPoint)point; 

//画直线
- (void)addLineToPoint:(CGPoint)point; 

//画曲线,分别对应终点和两个控制点
- (void)addCurveToPoint:(CGPoint)endPoint controlPoint1:(CGPoint)controlPoint1 controlPoint2:(CGPoint)controlPoint2;

//画圆弧
- (void)addArcWithCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise

您可能感兴趣的与本文相关的镜像

ACE-Step

ACE-Step

音乐合成
ACE-Step

ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值