我使用跟随CGPathAddEllipseInRect的CAKeyframeAnimation围绕圆圈制作UIView.但是,视图似乎总是在同一个地方开始,无论它最初定位在哪个帧中.是否有某种方法可以调整路径上视图的起始位置?
谢谢!
CAKeyframeAnimation *myAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
myAnimation.fillMode = kCAFillModeForwards;
myAnimation.repeatCount = 5;
myAnimation.calculationMode = kCAAnimationPaced;
myAnimation.duration = 10.0;
CGMutablePathRef animationPath = CGPathCreateMutable();
CGPathAddEllipseInRect(animationPath, NULL, rect);
myAnimation.path = animationPath;
CGPathRelease(animationPath);
[view.layer addAnimation:myAnimation forKey:@"changeViewLocation"];
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用iOS 5的粒子系统(CAEmitterLayer和CAEmitterCell)来围绕一个圆圈绘制粒子(甚至更好,一个CGPath),但我不知道该怎么做.我能做的最好的是制作一个弧(通过修改CAEmitterCell的yAcceleration属性),但我不能做一个完整的循环.当然,我可以做多个圆弧来模拟圆形,但"结"非常明显.此外,我不想使用蒙版,因为看起来边缘的粒子被裁剪.任何想法如何做到这一点?
我需要一些帮助来填补我的道路.我不知道在哪里CGContextFillPath(path);举例.我是绘图(和iOS开发)的新手,我现在已经尝试了近两个小时了.但是我达到了我必须放弃的程度......希望你能解释一下我的问题.我收到以下错误:invalid context
-(CGMutablePathRef) drawHexagon:(CGPoint)origin
{
//create mutable path
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, nil, origin.x, origin.y);
CGPoint newloc = CGPointMake(origin.x - 20, origin.y + 42);
CGPathMoveToPoint(path, NULL, newloc.x, newloc.y);
CGPathAddLineToPoint(path, NULL, newloc.x + 16,newloc.y + 38);
CGPathAddLineToPoint(path, NULL, newloc.x + 49, newloc.y + 0);
CGPathAddLineToPoint(path, NULL, newloc.x + 23, newloc.y - 39);
CGPathAddLineToPoint(path, NULL, newloc.x - 25,newloc.y - 40);
CGPathAddLineToPoint(path, NULL, newloc.x -43, newloc.y + 0);
CGPathCloseSubpath(path);
CGContextAddPath(context, path);
context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGContextSetLineWidth(context, 2.0);
CGContextSetStrokeColorWithColor(context, [UIColor …Run Code Online (Sandbox Code Playgroud) 我在正在处理的应用程序中遇到问题.假设我有两个相当复杂的CGPath,我将它们都添加到CGMutablePath(因此将它们组合在一起).那么,在两条路径相交的地方,将会有彼此内部的点.我想消除那些内部点,并基本上绘制路径的外部或轮廓.我很难搞清楚如何解决这个问题.
编辑:这是我正在谈论的一个例子.蓝色和红色框表示沿CGPath的点.红色框是两个路径中的点.我想以某种方式消除红点并重新绘制路径的轮廓.

我想在我的精灵工具包游戏中绘制一条虚线,我可以使用SKShapeNode节点绘制如下的法线:
UIBezierPath *_path=[UIBezierPath bezierPath];
//1
CGPoint point1 = CGPointMake(100,100);
CGPoint point2 = CGPointMake(150,150);
[_path moveToPoint:point1];
[_path addLineToPoint:point2];
//2
SKShapeNode *line = [SKShapeNode node];
line.path = _path.CGPath;
Run Code Online (Sandbox Code Playgroud)
我尝试将虚线模式设置为UIBezierPath,如下所示:
// adding this code at location 1 or 2 above but no effect
CGFloat dashes[] = {6, 2};
[_path setLineDash:dashes count:2 phase:0];
Run Code Online (Sandbox Code Playgroud)
但是没有应用虚线图案.
我还尝试直接从UIBezierpath.CGPath属性创建CGPath的虚线副本,如下所示:
CGFloat dashes[] = {6, 2};
CGPathRef aCGPath= CGPathCreateCopyByDashingPath(_path.CGPath,NULL,0,dashes,2);
line.path = aCGPath;
Run Code Online (Sandbox Code Playgroud)
但也一样.
我真的很感激,如果有人可以解释什么是问题,我怎么能通过将虚线cgpath应用到skshapenode在两点之间画一条虚线.
编辑:我知道这个简单的例子我可以将这两个点之间的距离分成小的固定距离,并通过bezeirpath移动和绘制虚线但是考虑一个带有点来自触摸的自由手路径,重绘是非常复杂和低效的具有固定长度点的路径然后绘制破折号.我想知道是否有办法将虚线模式应用于路径并使skshapenode使用它是我的问题.
所以,基本上,我有一个精灵(.png)SpriteKit,其中大多数像素是零/没有细节等.
我希望UIBezierPath只在Sprite中创建一个细节,所以以后我可以使用containsPoint.
经过一堆谷歌搜索,我发现你可以PhysicsBody使用精灵图像中的纹理路径创建一个,如下所示:
let texture = SKTexture(imageNamed: "myImage.png")
img.physicsBody = SKPhysicsBody(texture: texture, size: img.size)
Run Code Online (Sandbox Code Playgroud)
如果您知道我的意思,这会围绕图像内部的细节创建一个物理主体,不包括任何alpha值像素.
有没有办法创造一样的BezierPath做法?或者可能创建一个CGPath,然后用它来创建一个BezierPath?
原因是,我希望测试一个CGPoint是否在这个精灵中,而不使用physicsDelegate或physicsBodys.
提前致谢.
我有一个CAShapeLayer,它必须做一个简单的任务,在用户的手指指导下移动屏幕.
问题是运动太慢了.该层确实移动,但有一个滞后,感觉很慢.
我有另一个测试应用程序,其中移动UIImage并且没有任何延迟,图像立即移动.
我该怎么做才能克服这个问题?
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
currentPoint = [[touches anyObject] locationInView:self];
}
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
{
CGPoint activePoint = [[touches anyObject] locationInView:self];
CGPoint newPoint = CGPointMake(activePoint.x - currentPoint.x,activePoint.y - currentPoint.y);
curLayer.position = CGPointMake(shapeLayer.position.x+newPoint.x,shapeLayer.position.y+newPoint.y);
currentPoint = activePoint;
}
谢谢!
我正在尝试组合几幅UIBezierPath图纸.
我可以制作不同类型的图纸(直线,立方贝塞尔曲线,二次贝塞尔曲线),并且每个都可以填充或填充.我随机选择了绘图类型,我的目标是制作3个不同的绘图,这些绘图在一个点连接.
因此,第一条线路绘图结束时,第二条路径 - 可能是一个立方贝塞尔曲线 - 开始.在那里结束,第三个,也许是填充的线条图开始.
我有一个UIView正在尝试绘制的正方形,每个路径应该有自己的一部分UIView:第一个1/3,第二个和第三个.
我能用一个UIBezierPath对象创建它,还是需要创建3个不同的对象?如何让它们在同一点结束并开始?有没有办法用子路径做到这一点?
我在Swift中有这个简单的代码:
override var bounds : CGRect {
didSet {
if (bounds != oldValue) {
var path = CGPathCreateMutable()
CGPathAddEllipseInRect(path, nil, bounds)
self.path = path
CGPathRelease(path)
}
}
}
Run Code Online (Sandbox Code Playgroud)
当图层bounds发生变化时,它应该绘制一个填充图层的圆圈.
这是从我的旧Objective-C代码移植而来的,它运行正常:
- (void)setBounds:(CGRect)bounds
{
if (CGRectEqualToRect(self.bounds, bounds)) return;
super.bounds = bounds;
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddEllipseInRect(path, nil, bounds);
self.path = path;
CGPathRelease(path);
}
Run Code Online (Sandbox Code Playgroud)
但是,Swift代码崩溃了一些段错误.如果我不释放路径,那很好.如果我没有设置self.path = path,它也没关系(虽然没有任何东西会显示出来).如果两者都存在,它将崩溃.
我确实认为通过CGPathCreateMutable()需要释放创建的路径..而且似乎是ObjC的情况.有谁知道为什么它在Swift中不起作用?或者我做错了什么?
谢谢!
我想让 SKSpriteNodes 沿着字母轮廓移动。我有很多信件,但这是一个例子:
我希望精灵跟随红线。我发现这个答案主要涵盖了我的问题:Get path to trace out a character in an iOS UIFont
答案来自这个优秀且有效的示例代码:
let font = UIFont(name: "HelveticaNeue", size: 64)!
var unichars = [UniChar]("P".utf16)
var glyphs = [CGGlyph](count: unichars.count, repeatedValue: 0)
let gotGlyphs = CTFontGetGlyphsForCharacters(font, &unichars, &glyphs, unichars.count)
if gotGlyphs {
let cgpath = CTFontCreatePathForGlyph(font, glyphs[0], nil)!
let path = UIBezierPath(CGPath: cgpath)
print(path)
XCPlaygroundPage.currentPage.captureValue(path, withIdentifier: "glyph \(glyphs[0])")
}
Run Code Online (Sandbox Code Playgroud)
但是我仍然遇到一个问题,因为我的精灵没有完成所有字母的完整路径,而是例如“P”停在这里(并从底部开始):
我尝试添加一些点,path例如:
CGPathAddLineToPoint(path, nil, 0, 0)
Run Code Online (Sandbox Code Playgroud)
但结果可能不起作用,因为添加的点在<Close>语句之后:
<UIBezierPath: 0x7889ff70; <MoveTo {25.950001, 55.800003}>,
<LineTo {25.950001, 95.100006}>, …Run Code Online (Sandbox Code Playgroud) cgpath ×10
ios ×4
uibezierpath ×4
swift ×3
sprite-kit ×2
calayer ×1
cashapelayer ×1
drawing ×1
ios6 ×1
iphone ×1
objective-c ×1
particles ×1
quartz-2d ×1
skshapenode ×1
uikit ×1
xcode ×1