CGPath可以理解为图形的路径,在Objective-C工程中,其实系统定义的一个内部结构体,开发者不可以直接使用,开发者CGPathRef和CGMutablePathRef别名作为CGPath的引用 ,实际上,CGPathRef和CGMutablePathRef都是CGPath结构体类型的指针,不同的是一个是const类型不可修改的,一个是可以修改的,系统定义如下: typedef struct CGPath *CGMutablePathRef; typedef const struct CGPath *CGPathRef; 二、CGPath创建与内存管理的相关方法 关于CGPath的创建与内存管理的相关方法 ,列举如下: //这个方法获取CGPath类在CoreGraphics框架中的唯一标识 //CFTypeID 实际上是无符号整型的别名 其为CoreGraphics框架中每个类都定义了一个标识 CGPath 为280 CFTypeID CGPathGetTypeID(void); //这个方法创建一个srtuct CGPath * 指针 可以理解为可变的CGPath类 CGMutablePathRef CGPathCreateMutable
maskLayer = [[CAShapeLayer alloc] init]; maskLayer.frame = self.bounds; maskLayer.path = maskPath.CGPath maskLayer = [[CAShapeLayer alloc] init]; maskLayer.frame = self.bounds; maskLayer.path = maskPath.CGPath maskLayer = [[CAShapeLayer alloc] init]; maskLayer.frame = self.bounds; maskLayer.path = maskPath.CGPath maskLayer = [[CAShapeLayer alloc] init]; maskLayer.frame = self.bounds; maskLayer.path = maskPath.CGPath maskLayer = [[CAShapeLayer alloc] init]; maskLayer.frame = self.bounds; maskLayer.path = maskPath.CGPath
maskLayer = [[CAShapeLayer alloc] init]; maskLayer.frame = self.bounds; maskLayer.path = maskPath.CGPath maskLayer = [[CAShapeLayer alloc] init]; maskLayer.frame = self.bounds; maskLayer.path = maskPath.CGPath maskLayer = [[CAShapeLayer alloc] init]; maskLayer.frame = self.bounds; maskLayer.path = maskPath.CGPath maskLayer = [[CAShapeLayer alloc] init]; maskLayer.frame = self.bounds; maskLayer.path = maskPath.CGPath maskLayer = [[CAShapeLayer alloc] init]; maskLayer.frame = self.bounds; maskLayer.path = maskPath.CGPath
maskLayer = [[CAShapeLayer alloc] init]; maskLayer.frame = self.bounds; maskLayer.path = maskPath.CGPath maskLayer = [[CAShapeLayer alloc] init]; maskLayer.frame = self.bounds; maskLayer.path = maskPath.CGPath maskLayer = [[CAShapeLayer alloc] init]; maskLayer.frame = self.bounds; maskLayer.path = maskPath.CGPath maskLayer = [[CAShapeLayer alloc] init]; maskLayer.frame = self.bounds; maskLayer.path = maskPath.CGPath maskLayer = [[CAShapeLayer alloc] init]; maskLayer.frame = self.bounds; maskLayer.path = maskPath.CGPath
circle.path = \ [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 100, 100)].CGPath myShapeLayer]; } @end 你没看错, 就是这么简单 甚至不用创建UIView子类 我们讲下几个重要属性: path 可以看到,这里用的是UIBezierPath生成一个path,然后取他的CGPath 是一层对CGPath的封装,他更符合OC面向对象的语法风格。这都不是重点。 0, 100, 100); layer.position = self.view.center; layer.path = rectP.CGPath 100, 100); layer.position = self.view.center; layer.path = circleP.CGPath
200); _shapeLayer.position = self.view.center; _shapeLayer.path = [self getStarOneBezierPath].CGPath ); circleAnim.toValue = (__bridge id _Nullable)([self getStarTwoBezierPath].CGPath); _shapeLayer.path = [self getStarTwoBezierPath].CGPath; [_shapeLayer addAnimation:circleAnim forKey ); circleAnim.toValue = (__bridge id _Nullable)([self getStarOneBezierPath].CGPath); _shapeLayer.path = [self getStarOneBezierPath].CGPath; [_shapeLayer addAnimation:circleAnim forKey
path addLineToPoint:CGPointMake(10, 10)]; 73 //5、将路径添加到图形上下文中 74 CGContextAddPath(ctr, path.CGPath = [UIBezierPath bezierPathWithRect:CGRectMake(10, 90, 60, 60)]; 178 CGContextAddPath(ctr, path.CGPath startAngle:0 endAngle:M_PI clockwise:YES];//顺时针从2π到π,圆心(160,100) 202 CGContextAddPath(ctr, path6.CGPath endAngle3 clockwise:YES]; 254 [path3 addLineToPoint:center]; 255 CGContextAddPath(ctr, path3.CGPath = [UIBezierPath bezierPathWithRect:CGRectMake(0, 290, 300, 2)]; 344 CGContextAddPath(ctr, path.CGPath
这里就对在iOS中应用贝塞尔曲线进行简单的讨论,首先CoreGraphics核心图形框架中提供了CGPath可以直接创建贝塞尔曲线,系统支持的贝塞尔曲线函数有二阶与三阶。 CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise; //使用CGPath 创建 + (instancetype)bezierPathWithCGPath:(CGPathRef)CGPath; //CGPath对象 @property(nonatomic) CGPathRef CGPath; //将路径移动到某个点 - (void)moveToPoint:(CGPoint)point; //添加一天线 - (void)addLineToPoint:(CGPoint)point
图片来自网络 /** * 根据CGPath创建并返回一个新的UIBezierPath对象 * @param CGPath CGPathRef */ + (instancetype)bezierPathWithCGPath :(CGPathRef)CGPath; @property(nonatomic) CGPathRef CGPath; - (CGPathRef)CGPath NS_RETURNS_INNER_POINTER
CGSize){20.0}]; CAShapeLayer *shapeLayer = [CAShapeLayer layer]; // 设置绘制路径 shapeLayer.path = bezierPath.CGPath cornerRadius:20.0]; CAShapeLayer *shapeLayer = [CAShapeLayer layer]; // 设置绘制路径 shapeLayer.path = bezierPath.CGPath
不过有一个简单的方案,也可以使得我们不需要调用CFRelese函数,参考如下代码: CGMutablePathRef cgPath = CGPathCreateMutable(); …… CGPathRef path = (__bridge CGPathRef)(CFBridgingRelease(cgPath)); 通过上面两次的转换,我们就不需要手动调用CFRelease来释放内存了。
[self getSelectLeftLayerBezierPath].CGPath : [self getNormalLeftLayerBezierPath].CGPath); leftanimation.toValue [self getNormalLeftLayerBezierPath].CGPath : [self getSelectLeftLayerBezierPath].CGPath); _leftShape.path [self getNormalLeftLayerBezierPath].CGPath : [self getSelectLeftLayerBezierPath].CGPath; [_leftShape
添加路径到上下文 CGContextAddPath(ctx, path.CGPath); // 4.渲染上下文 CGContextStrokePath(ctx); 变换 添加路径到上下文 CGContextAddPath(ctx, path.CGPath); //保存上下文 CGContextSaveGState(ctx); //设置上下文状态 添加路径到上下文 CGContextAddPath(ctx, path.CGPath); //还原上下文 CGContextRestoreGState(ctx); //设置上下文状态
radius:width/2.0f startAngle:0 endAngle:M_PI*2 clockwise:YES]; backgroundLayer.path = backgroundPath.CGPath clockwise:YES]; frontFillLayer.path = frontFillPath.CGPath
contentWidth/2-2 startAngle:-M_PI_2 endAngle:M_PI_2*3 clockwise:YES]; _contentLayer.path = pathT.CGPath :_contentWidth/2-2 startAngle:-M_PI_2 endAngle:M_PI_2*3 clockwise:YES]; _shapeLayer.path = path.CGPath contentWidth/2-lineWidth/2 startAngle:-M_PI_2 endAngle:M_PI_2*3 clockwise:YES]; _shapeLayer.path = path.CGPath contentWidth/2-lineWidth/2 startAngle:-M_PI_2 endAngle:M_PI_2*3 clockwise:YES]; _contentLayer.path = pathT.CGPath
progressline addLineToPoint:point]; 绘制圆点 index += 1; } _chartLine.path = progressline.CGPath setLineWidth:1.0]; [progressline setLineCapStyle:kCGLineCapSquare]; _chartLine.path = progressline.CGPath circle.lineWidth = radius*2; circle.strokeStart = 0; circle.strokeEnd = 0.6; circle.path = path.CGPath = radius*2; circle1.strokeStart = 0.6; circle1.strokeEnd = 0.8; circle1.path = path.CGPath clearColor].CGColor; circle.strokeStart = 0; circle.strokeEnd = 0.6; circle.path = path.CGPath
100, startAngle: 0, endAngle: CGFloat.pi * 2, clockwise: true) shapeLayer.path = circularPath.cgPath let trackLayer = CAShapeLayer() trackLayer.path = circularPath.cgPath trackLayer.strokeColor 2, clockwise: true) let trackLayer = CAShapeLayer() trackLayer.path = circularPath.cgPath 2, clockwise: true) let trackLayer = CAShapeLayer() trackLayer.path = circularPath.cgPath radius: 100, startAngle: 0, endAngle: 2 * CGFloat.pi, clockwise: true) layer.path = circularPath.cgPath
kCGLineCapRound); //线条颜色 [_pathBackColor setStroke]; //把路径添加到上下文 CGContextAddPath(ctx, basePath.CGPath kCGLineCapRound); //线条颜色 [_pathFillColor setStroke]; //把路径添加到上下文 CGContextAddPath(ctx, valuePath.CGPath 89, 299, 138, 138)]; # 设置末端的形状为圆形 path12.lineCapStyle = kCGLineCapRound; shapeLayer11.path=path12.CGPath bezierPathWithOvalInRect:CGRectMake(89, 299, 138, 138)]; path1.lineCapStyle = kCGLineJoinRound; shapeLayer1.path = path1.CGPath
CAShapeLayer *maskLayer = [CAShapeLayer layer]; maskLayer.path = [self.path CGPath )point withEvent:(UIEvent *)event { // 判断点击的point 是否在六边形内 if (CGPathContainsPoint(self.path.CGPath
//创建maskLayer CAShapeLayer *maskLayer = [CAShapeLayer layer]; maskLayer.path = apath.CGPath endAngle:2 * M_PI clockwise:YES].CGPath endAngle:2 * M_PI clockwise:YES].CGPath