将矩阵转换为CGAffineTransform

时间:2019-02-07 20:43:22

标签: ios swift core-graphics

我已经在Android上开发了一个应用,您可以在屏幕上添加图像并进行平移/旋转/缩放。从技术上讲,我有一个长宽比固定的父级布局,其中包含这些图像(ImageViews)。他们将宽度和高度设置为match_parent。然后,我可以将它们的位置保存在服务器上,以后再将其加载到相同或其他设备上。基本上,我只是保存矩阵值。但是,转换x / y具有绝对值(以像素为单位),但是屏幕尺寸不同,所以我将它们设置为百分比值(因此,转换x除以父级宽度,转换y除以父级高度)。加载图像时,我只需将平移x / y乘以父级布局的宽度/高度,即使在不同的设备上,一切看起来也一样。

这是我的方法:

float[] values = new float[9];
parentLayout.matrix.getValues(values);

values[Matrix.MTRANS_X] /= parentLayout.getWidth();
values[Matrix.MTRANS_Y] /= parentLayout.getHeight();

然后在加载函数中,我简单地将这些值相乘。

通过使用带有锚点的postTranslate / postScale / postRotate进行图像操作(对于2指手势,这是这些手指之间的中点)。

现在,我想将此应用程序移植到iOS,并使用类似的技术在iOS设备上也获得正确的图像位置。 CGAffineTransform似乎与Matrix类非常相似。字段的顺序不同,但是它们的工作方式似乎相同。

让我们说这个数组是Android应用程序的样本值数组:

let a: [CGFloat] = [0.35355338, -0.35355338, 0.44107446, 0.35355338, 0.35355338, 0.058058262]

我不保存最后3个字段,因为它们始终为0、0和1,并且在iOS上甚至无法设置(文档也指出为[0,0,1])。这就是“转化”的样子:

let tx = a[2] * width //multiply percentage translation x with parent width
let ty = a[5] * height //multiply percentage translation y with parent height
let transform = CGAffineTransform(a: a[0], b: a[3], c: a[1], d: a[4], tx: tx, ty: ty)

但是,这仅适用于非常简单的情况。对于其他图像,图像通常会放错位置甚至完全弄乱。

我注意到,默认情况下,在Android上,我的锚点位于[0,0],但在iOS上,它是图像的中心。

例如:

float midX = parentLayout.getWidth() / 2.0f;
float midY = parentLayout.getHeight() / 2.0f;

parentLayout.matrix.postScale(0.5f, 0.5f, midX, midY);

给出以下矩阵:

0.5, 0.0, 360.0, 
0.0, 0.5, 240.0, 
0.0, 0.0, 1.0

[360.0,240.0]只是从左上角开始的向量。

但是,在iOS上,我不必提供中间点,因为它已经围绕中心进行了转换:

let transform = CGAffineTransform(scaleX: 0.5, y: 0.5)

它给了我

CGAffineTransform(a: 0.5, b: 0.0, c: 0.0, d: 0.5, tx: 0.0, ty: 0.0)

我尝试在iOS上设置其他锚点:

parentView.layer.anchorPoint = CGPoint(x: 0, y: 0)

但是,我无法获得正确的结果。我已经尝试过按-width * 0.5,-height * 0.5进行翻译,缩放并向后翻译,但是我得到的结果与Android上的结果不同。

简而言之:如何将Android的Matrix修改为iOS的CGAffineTransform以实现相同外观?

我还将附加尽可能简单的演示项目。目的是将Android中的输出数组复制到iOS项目中的“ a”数组中,并修改CGAffineTransform计算(和/或parentView设置),以便无论Android的日志输出如何,两个平台上的图像都相同。

Android: https://github.com/piotrros/MatrixAndroid

iOS: https://github.com/piotrros/MatrixIOS

编辑:

@Sulthan解决方案效果很好!尽管有,但我提出了一个逆向过程的子问题,这也是我所需要的。根据他的回答,我尝试将其合并到我的应用中:

let savedTransform = CGAffineTransform.identity
        .concatenating(CGAffineTransform(translationX: -width / 2, y: -height / 2))
        .concatenating(
            self.transform
        )
        .concatenating(CGAffineTransform(translationX: width / 2, y: height / 2))

let tx = savedTransform.tx
let ty = savedTransform.ty

let t = CGAffineTransform(a: savedTransform.a, b: savedTransform.b, c: savedTransform.c, d: savedTransform.d, tx: tx / cWidth, ty: ty / cHeight)

placement.transform = [Float(t.a), Float(t.c), Float(t.tx), Float(t.b), Float(t.d), Float(t.ty), 0, 0, 1]

placement只是一个用于保存矩阵的“ Android”版本的类。 cWidth / cHeight是父级的宽度/高度。我将tx / ty除以它们以获得宽度/高度百分比,就像在Android上一样。

但是,它不起作用。 tx / ty错了。

输入:

  

[0.2743883,-0.16761175、0.43753636、0.16761175、0.2743883,   0.40431038,0.0,0.0,1.0]

它回馈:

  

[0.2743883,-0.16761175、0.18834576、0.16761175、0.2743883、0.2182884,   0.0、0.0、1.0]

因此,除了tx / ty之外的其他所有内容都很好。错误在哪里?

2 个答案:

答案 0 :(得分:1)

第一个问题是,在Android中,您为父级布局设置了transform,然后影响子级。但是,在iOS上,您必须在imageView本身上设置转换。这对于以下两种解决方案都是常见的。

解决方案1:更新锚点

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    imageView.translatesAutoresizingMaskIntoConstraints = true
    imageView.bounds = parentView.bounds
    imageView.layer.anchorPoint = .zero
    imageView.center = .zero

    let width = parentView.frame.width
    let height = parentView.frame.height

    let a: [CGFloat] = [0.35355338, -0.35355338, 0.44107446, 0.35355338, 0.35355338, 0.058058262]

    let tx = a[2] * width
    let ty = a[5] * height
    let transform = CGAffineTransform(a: a[0], b: a[3], c: a[1], d: a[4], tx: tx, ty: ty)

    imageView.transform = transform
}

重要:我已从情节提要中删除了imageView的所有约束条件

我们必须将视图anchorPoint移动到图片的左上角以匹配Android。但是,非显而易见的问题是这也会影响视图的位置,因为视图位置是相对于锚点计算的。有了约束,它开始变得混乱,因此我删除了所有约束,并切换到手动定位:

imageView.bounds = parentView.bounds // set correct size
imageView.layer.anchorPoint = .zero // anchor point to top-left corner
imageView.center = .zero // set position, "center" is now the top-left corner

解决方案2:更新变换矩阵

如果我们不想碰到锚点,则可以保持约束不变,只需更新变换即可。

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    let width = parentView.frame.width
    let height = parentView.frame.height

    let a: [CGFloat] = [0.35355338, -0.35355338, 0.44107446, 0.35355338, 0.35355338, 0.058058262]

    let tx = a[2] * width
    let ty = a[5] * height
    let savedTransform = CGAffineTransform(a: a[0], b: a[3], c: a[1], d: a[4], tx: tx, ty: ty)

    let transform = CGAffineTransform.identity
        .translatedBy(x: width / 2, y: height / 2)
        .concatenating(savedTransform)
        .concatenating(CGAffineTransform(translationX: -width / 2, y: -height / 2))

    imageView.transform = transform
}

CGAffineTransform的一个显而易见的问题是事实

.concatenating(CGAffineTransform(translationX: -width / 2, y: -height / 2))

.translatedBy(x: -width / 2, y: -height / 2)

不一样。乘法顺序不同。

iOS

撤消该过程:

要生成用于Android的相同矩阵,我们只需移动原点:

let savedTransform = CGAffineTransform.identity
    .translatedBy(x: width / 2, y: height / 2)
    .scaledBy(x: 0.5, y: 0.5)
    .rotated(by: .pi / 4)
    .translatedBy(x: -width / 2, y: -height / 2)

或使用串联:

let savedTransform = CGAffineTransform.identity
    .concatenating(CGAffineTransform(translationX: -width / 2, y: -height / 2))
    .concatenating(
        CGAffineTransform.identity
            .scaledBy(x: 0.5, y: 0.5)
            .rotated(by: .pi / 4)
    )
    .concatenating(CGAffineTransform(translationX: width / 2, y: height / 2))

通过串联,很明显原点转换是如何抵消的。

摘要:

func androidValuesToIOSTransform() -> CGAffineTransform{
    let width = parentView.frame.width
    let height = parentView.frame.height

    let androidValues: [CGFloat] = [0.35355338, -0.35355338, 0.44107446, 0.35355338, 0.35355338, 0.058058262]
    let androidTransform = CGAffineTransform(
        a: androidValues[0], b: androidValues[3],
        c: androidValues[1], d: androidValues[4],
        tx: androidValues[2] * width, ty: androidValues[5] * height
    )

    let iOSTransform = CGAffineTransform.identity
        .concatenating(CGAffineTransform(translationX: width / 2, y: height / 2))
        .concatenating(androidTransform)
        .concatenating(CGAffineTransform(translationX: -width / 2, y: -height / 2))

    return iOSTransform
}

func iOSTransformToAndroidValues() -> [CGFloat] {
    let width = parentView.frame.width
    let height = parentView.frame.height

    let iOSTransform = CGAffineTransform.identity
        .scaledBy(x: 0.5, y: 0.5)
        .rotated(by: .pi / 4)
    let androidTransform = CGAffineTransform.identity
        .concatenating(CGAffineTransform(translationX: -width / 2, y: -height / 2))
        .concatenating(iOSTransform)
        .concatenating(CGAffineTransform(translationX: width / 2, y: height / 2))

    let androidValues = [
        androidTransform.a, androidTransform.c, androidTransform.tx / width,
        androidTransform.b, androidTransform.d, androidTransform.ty / height
    ]
    return androidValues
}

答案 1 :(得分:0)

这个答案怎么样:我添加了四个滑块,使事情更容易看清。

class ViewController: UIViewController {

@IBOutlet weak var parentView: UIView!
@IBOutlet weak var imageView: UIImageView!

override func viewDidLoad() {
    super.viewDidLoad()


}




let width =  UIScreen.main.bounds.width
let height = UIScreen.main.bounds.width * 2.0 / 3.0


var zoom: CGFloat = 1.0
var rotate: CGFloat = 0.0

 var locX: CGFloat = 0.0
 var locY: CGFloat = 0.0

@IBAction func rotate(_ sender: UISlider){  //0-6.28

    rotate = CGFloat(sender.value)
    parentView.transform = CGAffineTransform(rotationAngle: rotate).concatenating(CGAffineTransform(scaleX: zoom, y: zoom))
    parentView.transform.tx = (locX-0.5) * width
    parentView.transform.ty = (locY-0.5) * height
}

@IBAction func zoom(_ sender: UISlider){ //0.1 - 10

    zoom = CGFloat(sender.value)
    parentView.transform = CGAffineTransform(rotationAngle: rotate).concatenating(CGAffineTransform(scaleX: zoom, y: zoom))
    parentView.transform.tx = (locX-0.5) * width
    parentView.transform.ty = (locY-0.5) * height
}

@IBAction func locationX(_ sender: UISlider){  //0 - 1

    locX = CGFloat(sender.value)
    parentView.transform = CGAffineTransform(rotationAngle: rotate).concatenating(CGAffineTransform(scaleX: zoom, y: zoom))
    parentView.transform.tx = (locX-0.5) * width
    parentView.transform.ty = (locY-0.5) * height
}

@IBAction func locationY(_ sender: UISlider){ //0 - 1
    locY = CGFloat(sender.value)
    parentView.transform = CGAffineTransform(rotationAngle: rotate).concatenating(CGAffineTransform(scaleX: zoom, y: zoom))
    parentView.transform.tx = (locX-0.5) * width
    parentView.transform.ty = (locY-0.5) * height
}



override func viewWillAppear(_ animated: Bool) {
  let a: [CGFloat] = [0.35355338, -0.35355338, 0.44107446, 0.35355338, 0.35355338, 0.058058262]
    let tx = (a[2] - 0.5) * width
   let ty = (a[5] - 0.5) * height
let transform =  CGAffineTransform(a: a[0], b: a[3], c: a[1], d: a[4], tx:tx, ty: ty)
parentView.layer.anchorPoint   = CGPoint.zero
parentView.layer.position = CGPoint.zero
parentView.transform = transform

}

}