iOS 使用貝塞爾曲線(bezierPath)完成簡(jiǎn)單圖片的制作

簡(jiǎn)單圖片.png
創(chuàng)建路徑
+ (instancetype)bezierPath;

@property(nonatomic) CGFloat lineWidth;   線寬@property(nonatomic) CGLineCap lineCapStyle;  線條拐角
@property(nonatomic) CGLineJoin lineJoinStyle; 終點(diǎn)處理

- (void)moveToPoint:(CGPoint)point;  移動(dòng)到某一點(diǎn)

- (void)addLineToPoint:(CGPoint)point; 在這個(gè)點(diǎn)與上一個(gè)點(diǎn)直接添加線
根據(jù)三個(gè)點(diǎn)繪制一條曲線,和moveToPoint配合 
- (void)addCurveToPoint:(CGPoint)endPoint controlPoint1:(CGPoint)controlPoint1 controlPoint2:(CGPoint)controlPoint2;
根據(jù)兩個(gè)點(diǎn)繪制一條曲線,和moveToPoint配合

- (void)addQuadCurveToPoint:(CGPoint)endPoint controlPoint:(CGPoint)controlPoint;
繪制圓形 
(默認(rèn)0角度在中心的右邊) clockwise(YES為順時(shí)針)
#define  kDegreesToRadians(degrees)  ((3.14159265359 * degrees)/ 180)
- (void)addArcWithCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise;

- (void)fill;填充路徑所圍繞的區(qū)域
- (void)stroke;連接各個(gè)點(diǎn)

- (void)closePath;  關(guān)閉路徑

創(chuàng)建矩形路徑

+ (instancetype)bezierWithRect:(CGRect)rect;
創(chuàng)建內(nèi)切圓路徑
+ (instancetype)bezierPathWithOvalInRect:(CGRect)rect;
創(chuàng)建圓角矩形
+ (instancetype)bezierPathWithRoundedRect:(CGRect)rect cornerRadius:(CGFloat)cornerRadius;

創(chuàng)建某一位置圓角的矩形
+ (instancetype)bezierPathWithRoundedRect:(CGRect)rect byRoundingCorners:(UIRectCorner)corners cornerRadii:(CGSize)cornerRadii;

typedef NS_OPTIONS(NSUInteger, UIRectCorner) {    UIRectCornerTopLeft     = 1 << 0,    UIRectCornerTopRight    = 1 << 1,    UIRectCornerBottomLeft  = 1 << 2,    UIRectCornerBottomRight = 1 << 3,    UIRectCornerAllCorners  = ~0UL
};
創(chuàng)建圓形路徑
+ (instancetype)bezierPathWithArcCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise;
根據(jù)另一個(gè)路徑創(chuàng)建路徑
+ (instancetype)bezierPathWithCGPath:(CGPathRef)CGPath;

路徑的一些信息
@property(readonly,getter=isEmpty) BOOL empty;@property(nonatomic,readonly) CGRect bounds;@property(nonatomic,readonly) CGPoint currentPoint;
- (BOOL)containsPoint:(CGPoint)point;
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self customSubViews];
}
// 自定義子視圖
- (void)customSubViews{
    UIImage *image = [self getImagewithColor:[UIColor redColor] size:CGSizeMake(100, 100) centerViewRadius:25 centerAngle:60];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 100, 100, 100)];
    imageView.image = image;
    
    [self.view addSubview:imageView];
}
// 根據(jù)透明度繪制一個(gè)圖片
- (UIImage *)getImagewithColor:(UIColor *)color size:(CGSize)size centerViewRadius:(CGFloat)radius centerAngle:(CGFloat)angle{
    // 聲明一個(gè)繪制大小
    UIGraphicsBeginImageContext(size);
    UIBezierPath *path = [UIBezierPath bezierPath];
    path.lineWidth = 1;
    path.lineCapStyle = kCGLineCapRound;
    path.lineJoinStyle = kCGLineCapRound;
    [path addArcWithCenter:CGPointMake(size.width / 2 , cos(kDegreesToRadians(angle * 0.5)) * radius) radius:radius startAngle:kDegreesToRadians(-(90 - (angle * 0.5))) endAngle:kDegreesToRadians((270 - (angle * 0.5))) clockwise:YES];
    [path addLineToPoint:CGPointMake(0, 0)];
    [path addLineToPoint:CGPointMake(0, size.height)];
    [path addLineToPoint:CGPointMake(size.width, size.height)];
    [path addLineToPoint:CGPointMake(size.width, 0)];
    [path addLineToPoint:CGPointMake(size.width / 2 + sin(kDegreesToRadians(angle / 2)) * radius, 0)];
    [color set];
    [path stroke];
    // 聲明UIImage對(duì)象
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容