菜鸟教程小白 发表于 2022-12-11 22:42:13

ios - 如何保存应用于 UIView 的 CATransform3D


                                            <p><p>我有一个应用 CATransform3D 的 View (Uiview),
当我再次打开应用程序时,我想创建一个相同的 View </p>

<p>所以我这样做:
保存转换以便稍后在打开的应用中使用它</p>

<pre><code>   let radian_vertical =element.layer.value(forKeyPath: &#34;transform.rotation.y&#34;) as? NSNumber ?? 0
    let degree_vertical = (radian_vertical.floatValue) * 180 / (Float)(Double.pi)

    let radian_horizontal =element.layer.value(forKeyPath: &#34;transform.rotation.x&#34;) as? NSNumber ?? 0

   let degree_hori = (radian_horizontal.floatValue) * 180 / (Float)(Double.pi)
</code></pre>

<p>但我以后应用时它不正确</p>

<pre><code>//saved_degree_vertical and saved_degree_hori. is saved from degree_vertical and degree_hori

let radians_verti =saved_degree_vertical * (CGFloat)(Double.pi)/ 180
let radians_hori =saved_degree_hori * (CGFloat)(Double.pi)/ 180
let rotateVertical = CATransform3DRotate(newViewContent.layer.transform, CGFloat(radians_verti), 0, 1, 0)

let rotateHorizontal = CATransform3DRotate(newViewContent.layer.transform, CGFloat(radians_hori), 1, 0, 0)

newViewContent.layer.transform=rotateHorizontal
newViewContent.layer.transform =rotateVertical
//save degree
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您应该保存整个矩阵,而不仅仅是一些旋转。 <code>CATransform3D</code> 由 16 个值组成,因为它是 4x4 矩阵。这些属性被命名为 <code>m12</code>、<code>m13</code>、...、<code>m43</code>、<code>m44</code>。它还有一个构造函数来获取所有这些属性。</p>

<p>您甚至可以将所有 16 个值序列化为一个字符串,然后对其进行反序列化以使其更容易。查看类似以下内容:</p>

<pre><code>/// A method to save the current view matrix
func saveCurrentMatrix() {
    UserDefaults.standard.set(serializeViewMatrix(view: viewContent), forKey: &#34;my_saved_matrix&#34;)
}

/// A method to apply the saved matrix to current view
func loadCurrentMatrix() {
    do {
      try applyViewMatrixFromSerializedString(view: viewContent, string: UserDefaults.standard.string(forKey: &#34;my_saved_matrix&#34;))
    } catch {
      if let description = (error as NSError).userInfo[&#34;dev_info&#34;] as? String {
            print(&#34;Error occured applying saved matrix: \(description)&#34;)
      }
    }
}

/// Serializes a view.layer.transform matrix into string
///
/// - Parameter view: A view to save matrix from
/// - Returns: A string representation of the matrix. In format 1;0;0;0;0;1;0;0;0 ...
func serializeViewMatrix(view: UIView) -&gt; String {
    let matrix: CATransform3D = view.layer.transform
    let values: = [
      matrix.m11, matrix.m12, matrix.m13, matrix.m14,
      matrix.m21, matrix.m22, matrix.m23, matrix.m24,
      matrix.m31, matrix.m32, matrix.m33, matrix.m34,
      matrix.m41, matrix.m42, matrix.m43, matrix.m44
    ]
    return values.map { String(Double($0)) }.joined(separator: &#34;;&#34;)
}

/// Creates a matrix from string produced by serializeViewMatrix and applies it to given view
///
/// - Parameters:
///   - view: A view to apply matrix to
///   - string: A string representing the matrix
/// - Throws: Will throw on incorrect data
func applyViewMatrixFromSerializedString(view: UIView, string: String?) throws {
    guard let string = string else { return } // Simply return if there is no string

    // Genereate 16 string componets separated by &#34;;&#34;
    let components = string.components(separatedBy: &#34;;&#34;)
    guard components.count == 16 else { throw NSError(domain: &#34;Matrix serialization&#34;, code: 400, userInfo: [ &#34;dev_info&#34;: &#34;Incorrect number of components for matrix. Expected 16, got \(components.count)&#34; ]) }

    // Parse string compoenets to CGFloat values
    let values: = components.compactMap {
      guard let doubleValueRepresentation = Double($0) else { return nil }
      return CGFloat(doubleValueRepresentation)
    }
    guard values.count == 16 else { throw NSError(domain: &#34;Matrix serialization&#34;, code: 500, userInfo: [ &#34;dev_info&#34;: &#34;Unable to parse all values. \(components.count) out of 16 values were correctly paresed&#34; ]) }

    // Generate and apply transform
    view.layer.transform = CATransform3D(m11: values, m12: values, m13: values, m14: values,
                                       m21: values, m22: values, m23: values, m24: values,
                                       m31: values, m32: values, m33: values, m34: values,
                                       m41: values, m42: values, m43: values, m44: values)
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何保存应用于 UIView 的 CATransform3D,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/55701065/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/55701065/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何保存应用于 UIView 的 CATransform3D