OGeek|极客世界-中国程序员成长平台

标题: 使用 SpriteKit 渲染时,iOS UIBezierPath 是颠倒的? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 22:18
标题: 使用 SpriteKit 渲染时,iOS UIBezierPath 是颠倒的?

轨道路径是使用顶部/左侧原点 (0,0) 的数据点定义的。 从这些点创建一个 UIBezierPath

汽车沿路径的移动以距离的形式给出,并根据该距离计算百分比。 UIBezierPath 上的一个类别提供汽车沿路径的坐标。

#import "UIBezierPath-Points.h"
CGPoint currCoordinates = [self.trackPath pointAtPercent:percent withSlope:nil];

问题在于 SpriteKit 将轨道路径倒置。

SKScene 类中……

SKShapeNode *shapeNode = [[SKShapeNode alloc] init];
[shapeNode setPath:self.trackPath.CGPath];            <== path is rendered upside-down?
[shapeNode setPath:cgPath];
[shapeNode setStrokeColor:[UIColor blueColor]];
[self addChild:shapeNode];

我已尝试对 UiBezierPath 进行各种变换,但无法将坐标转换为我认为基于中心的 SpriteKit 坐标。

有谁知道如何将 UIBezierPath 坐标转换为 SpriteKit (SKScene) 坐标?

*不具备发布图片的 enuf 声誉。



Best Answer-推荐答案


对不起,我认为这是不言自明的。

来自 Source

void ApplyCenteredPathTransform(UIBezierPath *path, CGAffineTransform transform)
{
    CGRect rect = CGPathGetPathBoundingBox(path.CGPath); 
    CGPoint center = CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect));
    CGAffineTransform t = CGAffineTransformIdentity;
    t = CGAffineTransformTranslate(t, center.x, center.y);
    t = CGAffineTransformConcat(transform, t);
    t = CGAffineTransformTranslate(t, -center.x, -center.y);
    [path applyTransform:t];
}

void ScalePath(UIBezierPath *path, CGFloat sx, CGFloat sy)
{
    CGAffineTransform t = CGAffineTransformMakeScale(sx, sy);
    ApplyCenteredPathTransform(path, t);
}

然后

ScalePath(path, 1.0, -1.0);

关于使用 SpriteKit 渲染时,iOS UIBezierPath 是颠倒的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23788985/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4