我一直在敲打这个好几个小时。我试着阅读并搜索答案。我甚至尝试过"走开,然后回到它后面"方法,它没有工作。我甚至尝试过瑜伽和冥想!我没有胜过......
专家,请赐教。我试图获取一份不在drawRect中的CGPath副本:
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, ......);
CGPathAddLineToPoint(path, ....);
CGPathAddLineToPoint(path, ....);
CGPathAddLineToPoint(path, ....);
CGPathAddLineToPoint(path, ....);
最后:
CGPathCloseSubpath(path);
这不起作用,所以我尝试使用而不是上面的2个调用:
CGContextAddPath(context, path);
CGPathCreateMutableCopy(path);
最终 - 我只是希望能够获得此路径并将其作为CAShapeLayer中的路径插入并通过进行此调用来设置动画:
animation.toValue = [UIBezierPath bezierPathWithCGPath:path];
所以在我尝试在没有降落伞的情况下尝试另一项放松活动,例如Sky Diving,希望能够清醒我的思绪并最终能够做到这一点,请你帮我一下,向我展示一些例子?除此之外,您能否使用以下功能向我详细解释正确的方法:
CGPathCloseSubpath(path);
CGContextAddPath(context, path);
CGPathCreateMutableCopy(path);
提前致谢。
答案 0 :(得分:0)
CGPath是具有C风格API的C风格数据结构。基本上它是构成路径的点列表。
CGContext也是一种C风格的数据结构(可能更大一些)来绘制图像,即将点转换为彩色像素。
我将我的代码段放入一个函数中,您可以将其复制并粘贴到单个屏幕xcode项目中。您将在代码中找到解释作为注释,并且可以深入调试和探索CGPath对象的两个函数(我从https://github.com/erica/iOS-6-Cookbook获得了一些补充)。
#import "ViewController.h"
#import "OverlayView.h"
#import <QuartzCore/QuartzCore.h>
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// create a graphics context with the C-API of the QuartzCore framework
// the graphics context is only required for drawing the path
CGRect compositionBounds = CGRectMake(30, 30, 300, 508);
UIGraphicsBeginImageContext(compositionBounds.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
// create a mutable path with QuartzCore
CGMutablePathRef p1 = CGPathCreateMutable();
// create a CGPath with UIKit classes using Obj-C
UIBezierPath *bp = [UIBezierPath bezierPath];
[bp moveToPoint:(CGPoint){180, 120}];
[bp addLineToPoint:(CGPoint){30, 170}];
[bp addLineToPoint:(CGPoint){135, 175}];
[bp addLineToPoint:(CGPoint){105, 115}];
// if you fill the path during drawing it gets closed implicitly
// [bp closePath];
// add the CGPath to our mutable path
CGPathAddPath(p1, nil, bp.CGPath);
// add a closed triangle as subpath to our mutable path
CGPathMoveToPoint(p1, nil, 50, 250);
CGPoint p2[3];
p2[0] = CGPointMake(10, 20);
p2[1] = (CGPoint){210, 50};
p2[2] = (CGPoint){80, 120};
CGPathAddLines(p1, nil, p2, 3);
// explicitly close our last added subpath
CGPathCloseSubpath(p1);
// draw our mutable path (with all its subpaths) filling its area
CGContextAddPath(ctx, p1);
CGContextSetFillColorWithColor(ctx, [[UIColor redColor] CGColor]);
CGContextFillPath(ctx);
// draw our mutable path (with all its subpaths) stroking its outline
CGContextAddPath(ctx, p1);
CGContextSetRGBStrokeColor(ctx, 0, 0, 0, 1.0);
CGContextStrokePath(ctx);
// display our mutable path in an image view on screen
UIImage *i = UIGraphicsGetImageFromCurrentImageContext();
UIImageView *iv = [[UIImageView alloc] initWithImage:i];
[self.view addSubview:iv];
iv.frame = self.view.frame;
// release ressources created with the C-API of QuartzCore
CGPathRelease(p1);
UIGraphicsEndImageContext();
// create a mutable copy of a path and don't use it for drawing
CGMutablePathRef p3 = CGPathCreateMutableCopy(bp.CGPath);
NSArray *p3points = [self pointsFromCGPath:p3];
for (NSValue *point in p3points) {
NSLog(@"path element in p3: %@", NSStringFromCGPoint(point.CGPointValue));
}
// note that UIBezierPath takes care of its path itself
CGPathRelease(p3);
}
// modified from https://github.com/erica/iOS-6-Cookbook
#define VALUE(_INDEX_) [NSValue valueWithCGPoint:points[_INDEX_]]
void getPointsFromBezier(void *info, const CGPathElement *element)
{
NSMutableArray *bezierPoints = (__bridge NSMutableArray *)info;
// Retrieve the path element type and its points
CGPathElementType type = element->type;
CGPoint *points = element->points;
switch (type) {
case kCGPathElementMoveToPoint:
NSLog(@"MoveToPoint (%3.2f, %3.2f", points->x, points->y);
break;
case kCGPathElementAddLineToPoint:
NSLog(@"AddLineToPoint (%3.2f, %3.2f)", points->x, points->y);
break;
case kCGPathElementAddQuadCurveToPoint:
NSLog(@"AddQuadCurveToPoint (%3.2f, %3.2f), (%3.2f, %3.2f)", points->x, points->y, points[1].x, points[1].y);
break;
case kCGPathElementAddCurveToPoint:
NSLog(@"AddCurveToPoint (%3.2f, %3.2f), (%3.2f, %3.2f), (%3.2f, %3.2f)", points->x, points->y, points[1].x, points[1].y, points[2].x, points[2].y);
break;
case kCGPathElementCloseSubpath:
NSLog(@"CloseSubpath (%3.2f, %3.2f)", points->x, points->y);
break;
default:
NSLog(@"unknown");
break;
}
// Add the points if they're available (per type)
if (type != kCGPathElementCloseSubpath)
{
[bezierPoints addObject:VALUE(0)];
if ((type != kCGPathElementAddLineToPoint) &&
(type != kCGPathElementMoveToPoint))
[bezierPoints addObject:VALUE(1)];
}
if (type == kCGPathElementAddCurveToPoint)
[bezierPoints addObject:VALUE(2)];
}
- (NSArray *)pointsFromCGPath:(CGPathRef)path
{
NSMutableArray *points = [NSMutableArray array];
CGPathApply(path, (__bridge void *)points, getPointsFromBezier);
return points;
}