iOS DeveloperiOS 开发成长中心

iOS 使用贝塞尔曲线(bezierPath)完成简单图片的制作

2016-10-21  本文已影响1346人  LYSNote
简单图片.png
创建路径
+ (instancetype)bezierPath;

@property(nonatomic) CGFloat lineWidth;   线宽@property(nonatomic) CGLineCap lineCapStyle;  线条拐角
@property(nonatomic) CGLineJoin lineJoinStyle; 终点处理

- (void)moveToPoint:(CGPoint)point;  移动到某一点

- (void)addLineToPoint:(CGPoint)point; 在这个点与上一个点直接添加线
根据三个点绘制一条曲线,和moveToPoint配合 
- (void)addCurveToPoint:(CGPoint)endPoint controlPoint1:(CGPoint)controlPoint1 controlPoint2:(CGPoint)controlPoint2;
根据两个点绘制一条曲线,和moveToPoint配合

- (void)addQuadCurveToPoint:(CGPoint)endPoint controlPoint:(CGPoint)controlPoint;
绘制圆形 
(默认0角度在中心的右边) clockwise(YES为顺时针)
#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;填充路径所围绕的区域
- (void)stroke;连接各个点

- (void)closePath;  关闭路径

创建矩形路径

+ (instancetype)bezierWithRect:(CGRect)rect;
创建内切圆路径
+ (instancetype)bezierPathWithOvalInRect:(CGRect)rect;
创建圆角矩形
+ (instancetype)bezierPathWithRoundedRect:(CGRect)rect cornerRadius:(CGFloat)cornerRadius;

创建某一位置圆角的矩形
+ (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
};
创建圆形路径
+ (instancetype)bezierPathWithArcCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise;
根据另一个路径创建路径
+ (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];
}
// 根据透明度绘制一个图片
- (UIImage *)getImagewithColor:(UIColor *)color size:(CGSize)size centerViewRadius:(CGFloat)radius centerAngle:(CGFloat)angle{
    // 声明一个绘制大小
    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对象
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}
上一篇下一篇

猜你喜欢

热点阅读