我有一个任意的CGPath
,我想找到它的地理中心.我可以得到路径边界框,CGPathGetPathBoundingBox
然后找到该框的中心.但有没有更好的方法来找到路径的中心?
对于那些喜欢看代码的人的更新:这里是使用Adam在答案中建议的平均点数方法的代码(不要错过下面答案中更好的技术)...
BOOL moved = NO; // the first coord should be a move, the rest add lines
CGPoint total = CGPointZero;
for (NSDictionary *coord in [polygon objectForKey:@"coordinates"]) {
CGPoint point = CGPointMake([(NSNumber *)[coord objectForKey:@"x"] floatValue],
[(NSNumber *)[coord objectForKey:@"y"] floatValue]);
if (moved) {
CGContextAddLineToPoint(context, point.x, point.y);
// calculate totals of x and y to help find the center later
// skip the first "move" point since it is repeated at the end in this …
Run Code Online (Sandbox Code Playgroud) 是否可以在视图上重新定位已绘制的CGPath/UIBezierPath?我想移动或改变路径的位置然后可能回想起drawinRect方法再次显示图形.
我有一些CGPath
(一个带有两个圆圈的聚合物)在一个上进行CAShapeLayer
.
我可以修剪/减去这样的路径吗?
我正在使用CGContext进行绘图.我目前正在使用像这样的png文件来屏蔽绘图:
UIImage * myImage = [UIImage imageNamed:@"frame.png"];
CGContextRef context = UIGraphicsGetCurrentContext ();
CGContextClipToMask(context, self.view.bounds, myImage.CGImage);
Run Code Online (Sandbox Code Playgroud)
这很好,但现在我想使用现有的CGPathRef作为我的掩码.我应该看看如上所述将CGPathRef转换为UIImage和掩码吗?如果是这样,我将如何进行转换? - 或者 - 有更好的方法来解决这个问题吗?
当我创建一个显式动画来将CAShapeLayer的路径值从椭圆更改为矩形时,我遇到了一个问题.
在我的画布控制器中,我设置了一个基本的CAShapeLayer并将其添加到根视图的图层:
CAShapeLayer *aLayer;
aLayer = [CAShapeLayer layer];
aLayer.frame = CGRectMake(100, 100, 100, 100);
aLayer.path = CGPathCreateWithEllipseInRect(aLayer.frame, nil);
aLayer.lineWidth = 10.0f;
aLayer.strokeColor = [UIColor blackColor].CGColor;
aLayer.fillColor = [UIColor clearColor].CGColor;
[self.view.layer addSublayer:aLayer];
Run Code Online (Sandbox Code Playgroud)
然后,当我为路径设置动画时,在动画的最后几帧中,当形状变为矩形时会出现奇怪的毛刺/闪烁,而在前几帧中,当动画远离矩形时,会出现奇怪的毛刺/闪烁.动画设置如下:
CGPathRef newPath = CGPathCreateWithRect(aLayer.frame, nil);
[CATransaction lock];
[CATransaction begin];
[CATransaction setAnimationDuration:5.0f];
CABasicAnimation *ba = [CABasicAnimation animationWithKeyPath:@"path"];
ba.autoreverses = YES;
ba.fillMode = kCAFillModeForwards;
ba.repeatCount = HUGE_VALF;
ba.fromValue = (id)aLayer.path;
ba.toValue = (__bridge id)newPath;
[aLayer addAnimation:ba forKey:@"animatePath"];
[CATransaction commit];
[CATransaction unlock];
Run Code Online (Sandbox Code Playgroud)
我尝试了许多不同的东西,比如锁定/解锁CATransaction,玩各种填充模式等等......
这是故障的图像:http: //www.postfl.com/outgoing/renderingglitch.png
可以在此处找到我正在体验的视频:http: //vimeo.com/37720876
我正在尝试绘制一个中风的CGPath.
基本上我想用CGPath绘制一条线.然后我想回去在最后一个CGPath的两边绘制线条,使其具有轮廓效果.
这条线可以任何方式弯曲和转弯,但我总是需要外面的两条线来跟随.
编辑:我需要能够使线条的中间透明,但轮廓是纯黑色.
我需要不断创建一个cgpath.目前我这样做:
func createLine(){
var rand = randomBetweenNumbers(1, 2)
currentY--
if rand < 1.5{
currentX--
CGPathAddLineToPoint(leftPath, nil, currentX, currentY)
}else{
currentX++
CGPathAddLineToPoint(leftPath, nil, currentX, currentY)
}
CGPathAddLineToPoint(rightPath, nil, currentX+tileSize, currentY)
lineNode.path = leftPath
rightNode.path = rightPath
}
Run Code Online (Sandbox Code Playgroud)
并称之为:
NSTimer.scheduledTimerWithTimeInterval(0.05, target: self, selector: Selector("startTile"), userInfo: nil, repeats: true)
Run Code Online (Sandbox Code Playgroud)
但问题是,帧随着时间的推移越来越低.有什么我必须改变,以便帧率不再下降?
我的目标是创建一个随机的无尽路径.
如何将图像轮廓视为CGPath
?
例如,如何获取Google徽标的大纲(具有透明背景).这可能是稍微复杂的情况.因此,处理不太复杂的场景的解决方案也适用于我.
我不知道如何使用CGPath创建半圆路径.有我的代码:
let path = CGPathCreateMutable()
let Width: CGFloat = 38.0
let radius: CGFloat = (Width / 2.0) + (Treshold / 2.0)
CGPathAddArc(pathOne, nil, x, 0.0, radius, firstPoint.position.x, secondPoint.position.x, true)
let waitAction = SKAction.waitForDuration(1.0)
let moveAction = SKAction.followPath(pathOne, duration: 3.0)
capOne.runAction(SKAction.sequence([waitAction, moveAction]))
Run Code Online (Sandbox Code Playgroud) 我正在使用copy(strokingWithWidth:lineCap:lineJoin:miterLimit:transform :)来偏移CGPath .问题是偏移路径引入了似乎是斜接连接的各种锯齿状线.将更改设置miterLimit
为0无效,使用斜角线连接也没有任何区别.
在此图像中,有原始路径(在应用之前strokingWithWidth
),使用斜接连接的偏移路径,以及使用斜角连接的偏移路径.为什么不使用斜角连接有什么影响?
使用斜接的代码(注意使用CGLineJoin.round
产生相同的结果):
let pathOffset = path.copy(strokingWithWidth: 4.0,
lineCap: CGLineCap.butt,
lineJoin: CGLineJoin.miter,
miterLimit: 20.0)
context.saveGState()
context.setStrokeColor(UIColor.red.cgColor)
context.addPath(pathOffset)
context.strokePath()
context.restoreGState()
Run Code Online (Sandbox Code Playgroud)
使用斜角的代码:
let pathOffset = path.copy(strokingWithWidth: 4.0,
lineCap: CGLineCap.butt,
lineJoin: CGLineJoin.bevel,
miterLimit: 0.0)
context.saveGState()
context.setStrokeColor(UIColor.red.cgColor)
context.addPath(pathOffset)
context.strokePath()
context.restoreGState()
Run Code Online (Sandbox Code Playgroud) cgpath ×10
ios ×7
swift ×3
objective-c ×2
sprite-kit ×2
cashapelayer ×1
cocoa-touch ×1
drawing ×1
frame-rate ×1
infinite ×1
iphone ×1
skaction ×1
subtraction ×1
swift3 ×1
uibezierpath ×1
uiimage ×1