【问题标题】:Creating a Matrix Stack for iOS: Resetting Matrix to Identity为 iOS 创建矩阵堆栈:将矩阵重置为标识
【发布时间】:2021-10-19 17:15:48
【问题描述】:

我正在使用 Apple 的 Core Graphics 框架并尝试实现推送和弹出矩阵,以使用堆栈结构隔离和封装对当前转换矩阵 (ctm) 的更改。

这将在 Core Graphics 的 .saveGState().restoreGState() 之外,其原因是 .saveGState().restoreGState() 推送和弹出不仅仅是 ctm,我想提供我的 API /package 用户在推送和弹出时分离样式和矩阵的选项。

我想实现的流程是:

  1. pushMatrix() - 将 ctm 添加到堆栈。
  2. 对 ctm 进行任何缩放、平移、旋转或应用自定义仿射变换。
  3. popMatrix() - 将当前 ctm 更改回上次推送的状态。

这是我当前使用的代码(为简洁起见,减去堆栈代码):

public func resetMatrixToIdentity() {
    let inverted = context?.ctm.inverted()
    context?.ctm.concatenating(inverted!)
}
    
open func pushMatrix() {
    let currentTransformation = (context?.ctm)!
    matrixStack.push(matrix: currentTransformation)
}
    
open func popMatrix() {
    resetMatrixToIdentity()
    context?.ctm.concatenating(matrixStack.pop()!)
}

我遇到的问题是这段代码目前实际上根本不影响 CTM,所以我想知道我对 Core Graphics 或矩阵乘法中的 CTM 有什么误解。

我知道 previous post 并尝试在我的代码中应用这些见解,但它似乎不起作用。

【问题讨论】:

    标签: ios swift matrix core-graphics matrix-multiplication


    【解决方案1】:

    这是一个非常容易解决的问题,所以我会回答我自己的问题,以防其他人也犯这个错误。

    上下文的 ctm 属性只能获取。为了使用连接设置 ctm,我正在尝试:

    context?.ctm.concatenating(inverted!)
    

    这是不正确的。 Core Graphics用来连接的函数其实是:

    context?.concatenate(inverted!)
    

    一旦我更换了它,一切都按预期工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-16
      • 1970-01-01
      • 2021-12-05
      • 2013-04-24
      • 2013-09-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多