gpt4 book ai didi

ios - Convert UIView 转换从 View A 到 View B

转载 作者:行者123 更新时间:2023-11-28 15:08:48 28 4
gpt4 key购买 nike

将一个 View 的转换相对于另一个 View 的透视转换的最佳方式是什么?


示例:

View A 有 2 个 child , View B 和 View C。 View B 也有一个 child , View D。

View Hierarchy
A
/ \
B C
/
D

B 上的变换不是identity,D 是“identity”,但前提是孤立地看待 D(不考虑 B 上的变换)。

相对而言, View C 必须知道 View D 的变换是什么。在 C 视角下,D 不是 identity


我一直在考虑 UICoordinateSpace 和它的 convert 方法。我如何为转换创建类似的隐蔽方法?

非常感谢您的帮助:)

最佳答案

@Brandon 建议我应该使用 UICoordinateSpace convert 方法将 View 的框架转换为另一个 View 的 CoordinateSpace,并从那里获得框架差异的转换。该解决方案对我有用,但请注意,这没有考虑 rotationAngle,而是缩放变换以适应旋转的框架,因为它没有旋转。

解决方案

extension UIView {

/// Returns transform for translation and scale difference from self and given view.
func convertScaleAndTranslation(to view: UIView) -> CGAffineTransform {
return CGAffineTransform.from(frame, to: convert(frame, to: view))
}
}

extension CGAffineTransform {

/// Returns transform for translation and scale difference from two given rects.
static func from(_ from: CGRect, to: CGRect) -> CGAffineTransform {
let sx = to.size.width / from.size.width
let sy = to.size.height / from.size.height

let scale = CGAffineTransform(scaleX: sx, y: sy)

let heightDiff = from.size.height - to.size.height
let widthDiff = from.size.width - to.size.width

let dx = to.origin.x - widthDiff / 2 - from.origin.x
let dy = to.origin.y - heightDiff / 2 - from.origin.y
let trans = CGAffineTransform(translationX: dx, y: dy)

return scale.concatenating(trans)
}
}

@Brandon 谢谢你的帮助:)

关于ios - Convert UIView 转换从 View A 到 View B,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47997910/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com