我CGPathAddEllipseInRect用来绘制一个圆,然后在CAKeyframeAnimation中使用它.我的问题是动画总是在同一个地方开始.我认为我可以使用CGAffineTransform执行以下操作,使其从不同的点开始:
CGAffineTransform temp = CGAffineTransformMakeRotation(M_PI / 2);
CGPathAddEllipseInRect(animationPath , &temp, rect);
Run Code Online (Sandbox Code Playgroud)
我不知道这是做什么的.当它运行时,我甚至看不到动画的这一部分.它正在做一些屏幕外的事情.任何帮助理解这将是伟大的.
我使用CATiledLayer作为我的UIView的支持层,我把它放在UIScrollView中.在我的视图的init方法中,我正在创建绘制简单线条的CGPathRef对象.当我尝试在drawLayer:inContext中绘制此路径时,当我滚动/缩放时,它偶尔会与EXEC_BAD_ACCESS(很少)崩溃.
代码很简单,我只使用标准的CG*函数:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
CATiledLayer *tiledLayer = (CATiledLayer *)[self layer];
tiledLayer.levelsOfDetail = 10;
tiledLayer.levelsOfDetailBias = 5;
tiledLayer.tileSize = CGSizeMake(512.0, 512.0);
CGMutablePathRef mutablePath = CGPathCreateMutable();
CGPathMoveToPoint(mutablePath, nil, 0, 0);
CGPathAddLineToPoint(mutablePath, nil, 700, 700);
path = CGPathCreateCopy(mutablePath);
CGPathRelease(mutablePath);
}
return self;
}
+ (Class) layerClass {
return [CATiledLayer class];
}
- (void) drawRect:(CGRect)rect {
}
- (void) drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
CGContextSetRGBFillColor(ctx, 1, 1, 1, 1);
CGContextFillRect(ctx, self.bounds);
CGContextSetLineWidth(ctx, 5);
CGContextAddPath(ctx, path);
CGContextDrawPath(ctx, kCGPathStroke); …Run Code Online (Sandbox Code Playgroud) 我想沿路径设置对象的动画.我得到了它的工作,但运动不是线性的.(它在路径中的小弯道上减速很多.)
以下是主要步骤.
1)使用PockeSVG从SVG文件导入路径
-(CGMutablePathRef)makeMutablePathFromSVG:(NSString *)svgFileName{
PocketSVG *myVectorDrawing0 = [[PocketSVG alloc] initFromSVGFileNamed:svgFileName];
UIBezierPath *myBezierPath0 = myVectorDrawing0.bezier;
return CGPathCreateMutableCopy([myBezierPath0 CGPath]);
}
Run Code Online (Sandbox Code Playgroud)
2)创建CAKeyFrameAnimation
CAKeyframeAnimation *moveAlongPath = [CAKeyframeAnimation animationWithKeyPath:@"position"];
CGMutablePathRef animationPath = CGPathCreateMutableCopy(pathForwardTo5);
[moveAlongPath setPath:animationPath];
[moveAlongPath setDuration:1.0f];
moveAlongPath.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
moveAlongPath.delegate = self;
[[trackButton layer] addAnimation:moveAlongPath forKey:@"moveButtonAlongPath"];
CFRelease(animationPath);
Run Code Online (Sandbox Code Playgroud)
我尝试使用其他CAMediaTimingFunctions,他们都表现出这种行为.
问题:动画本身有效,但它并不遵循平滑一致的速度.知道为什么吗?
我正在尝试制作一个为用户绘制对象的应用程序.我现在有一个这样的对象,它是由一个类型的数组构成的[UIBezierPath].然后我使用循环UIBezierPath将数组中的所有s 更改为CGPaths,然后想要逐个绘制那些路径的动画.但是,当我尝试使用此代码时,它不起作用,我无法在网上找到任何有用的信息.这是代码被使用来转换阵列macbookPath它由UIBezierPath成CGPath:
for path in macbookPath {
drawingPath.append(path.cgPath)
}
Run Code Online (Sandbox Code Playgroud)
然后我使用此代码尝试绘制路径:
for cgPath in drawingPath {
shapeLayer.path = cgPath
}
Run Code Online (Sandbox Code Playgroud)
这是函数的其余代码,drawForm()它应该将表单绘制到一个名为的视图上aiDrawView:
@objc func drawForm() {
var drawingPath = [CGPath]()
var macbookPath = Forms.MacbookForm()
let shapeLayer = CAShapeLayer()
shapeLayer.frame = aiDrawView.bounds
for path in macbookPath {
drawingPath.append(path.cgPath)
}
for cgPath in drawingPath {
shapeLayer.path = cgPath
}
let strokeEndAnimation = CABasicAnimation(keyPath: "strokeEnd")
strokeEndAnimation.duration = 2.0
strokeEndAnimation.fromValue …Run Code Online (Sandbox Code Playgroud) 我需要根据图像(任何可能的形状)创建蒙版.我需要:
所以主要的问题是如何基于黑白图像设置CGPath?或者是否可以直接从图像中放置面具?
谢谢!
面具的示例图像:

我在CALayers中绘制了大约20个简单的形状,如下面的那个(在CAShapelayer中绘制的CGPath).现在我有一个非常长的文件,包含这种格式的所有20个形状.当创建父UIView时,它会根据前一个屏幕中选择的内容加载其中一个形状.这样做很好,但保持形状代码太麻烦了.将下面的代码块存储到单个文件中的最佳方法是什么?我可以在需要时调用并执行它(例如:star.txt,apple.txt,moon.txt,tree.txt):
CALayer* root = [[CALayer alloc] init];
root.name = nil;
root.bounds = CGRectMake(0.000000, 0.000000, 768.000000, 768.000000);
root.frame = CGRectMake(0.000000, 0.000000, 768.000000, 768.000000);
[root addSublayer:root];
[root release];
CALayer* root_layer1 = [[CALayer alloc] init];
root_layer1.name = nil;
root_layer1.bounds = CGRectMake(0.000000, 0.000000, 768.000000, 768.000000);
root_layer1.frame = CGRectMake(0.000000, 0.000000, 768.000000, 768.000000);
[someUIView.layer addSublayer:root_layer1];
[root_layer1 release];
CAShapeLayer* root_layer1_layer2 = [[CAShapeLayer alloc] init];
CGMutablePathRef root_layer1_layer2_path_pathref = CGPathCreateMutable();
CGPathMoveToPoint(root_layer1_layer2_path_pathref, NULL, 415.493011, 49.774002);
CGPathAddLineToPoint(root_layer1_layer2_path_pathref, NULL, 448.989014, 153.523010);
CGPathAddCurveToPoint(root_layer1_layer2_path_pathref, NULL, 458.575012, 183.251007, 485.170044, 202.583008, 516.384033, 202.510010);
CGPathAddLineToPoint(root_layer1_layer2_path_pathref, NULL, 625.388062, …Run Code Online (Sandbox Code Playgroud) 我有一个 [UIBezierPath] 类型的数组,我将其转换为 cgPaths,然后将其动画到我称为 shapeLayer 的 CAShapeLayer 上。现在由于某种原因我所有的路径都是颠倒的,所以所有的路径都被颠倒了。我该如何解决这个问题,我尝试了几种方法,但遗憾的是它们都不起作用......但是我确实弄清楚了如何扩展路径。这是我绘制 swiftPath 的代码,它是由函数 swiftBirdForm() 下的 Forms 类中找到的 UIBezierPaths 组成的路径。绘制路径工作正常,我只是不知道如何将其翻转 180 度。
@objc func drawForm() {
var swiftPath = Forms.swiftBirdForm()
let shapeLayer = CAShapeLayer()
shapeLayer.fillColor = UIColor.clear.cgColor
shapeLayer.strokeColor = UIColor.black.cgColor
shapeLayer.lineWidth = 1
shapeLayer.frame = CGRect(x: -120, y: 120, width: 350, height: 350)
var paths: [UIBezierPath] = swiftPath
guard let path = paths.first else {
return
}
paths.dropFirst()
.forEach {
path.append($0)
}
shapeLayer.transform = CATransform3DMakeScale(0.6, 0.6, 0)
shapeLayer.path = path.cgPath
self.view.layer.addSublayer(shapeLayer)
let strokeEndAnimation = …Run Code Online (Sandbox Code Playgroud) cgpath ×7
cashapelayer ×2
ios ×2
swift ×2
uibezierpath ×2
calayer ×1
catiledlayer ×1
cocoa ×1
cocoa-touch ×1
ipad ×1
iphone ×1
mask ×1
objective-c ×1
svg ×1
uiview ×1