图形翻转后如何正确定位

问题描述 投票:0回答:1

对于以下闪电图形:

我试图在缩放时翻转它,如下所示:

CGAffineTransform transform = CGAffineTransformMakeScale(newSize.width / initialSize.width, -1 * (newSize.height / initialSize.height));
CGMutablePathRef mutablePath = CGPathCreateMutableCopyByTransformingPath(shapeLayer.path, &transform);
shapeLayer.path = mutablePath;
CGPathRelease(mutablePath);

事情是这样的:

我尝试了很多不同的方法来翻译它,但没有成功:

CGAffineTransform transform = CGAffineTransformMakeTranslation(newSize.width / 2, newSize.height / 2);
transform = CGAffineTransformScale(transform, newSize.width / initialSize.width, -1 * (newSize.height / initialSize.height));
transform = CGAffineTransformTranslate(transform, -newSize.width / 2, -newSize.height / 2);

有人知道让我的图形回到视图边界框的正确方法吗?

ios objective-c core-graphics cgaffinetransform
1个回答
0
投票

不清楚您要做什么,并且您甚至没有提供足够的代码来生成您所描绘的界面,更不用说发现问题可能是什么了。然而,最简单的事情可能是变换形状图层,而不是路径。下面的代码在屏幕中央绘制一个非对称形状,然后水平翻转并放大它,围绕其中心进行操作,使其仍然位于屏幕中央:

    let shape = CAShapeLayer()
    shape.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
    let path = UIBezierPath()
    path.move(to: .zero)
    path.addLine(to: .init(x: 0, y: 100))
    path.addLine(to: .init(x: 100, y: 100))
    shape.path = path.cgPath
    self.view.layer.addSublayer(shape)
    shape.frame.origin = CGPoint(x: view.bounds.width / 2 - 50, y: view.bounds.height / 2 - 50)

    let newsize = 1.5
    DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
        shape.transform = CATransform3DMakeScale(-newsize, newsize, 0)
    }
© www.soinside.com 2019 - 2024. All rights reserved.