我遇到CATransform3DTranslate问题。我使用来自KennyTM here
的代码将它与CATransform3D连接起来我使用Photoshop测量坐标。这是我的完整代码:
background.image = UIImage(named: "backgroundtest")
gambar.image = UIImage(named: "imagetest")
gambar.layer.anchorPoint = CGPointZero
var translate = CATransform3DIdentity
translate = CATransform3DTranslate(translate, -160, -85.5, 0)
var perspective = CATransform3DIdentity
perspective = compute_transform_matrix(0.0, Y: 0.0, W: 320.0, H: 171.0, x1a: 81.5, y1a: 81.48, x2a: 294.44, y2a: 125.66, x3a: 22.02, y3a: 161.66, x4a: 270.21, y4a: 221.11)
gambar.layer.transform = CATransform3DConcat(perspective, translate)
并且该代码在iPhone 4s上完美运行 - iPhone 5s。但是当我使用iPhone 6或6 Plus模拟器时,它看起来并不正确。 here is the screenshot(红色矩形必须完全在绿色部分)。和here没有CATransform3DTranslate
这是compute_transform_matrix
的代码:
func compute_transform_matrix(X:CGFloat, Y:CGFloat, W:CGFloat, H:CGFloat, x1a:CGFloat, y1a:CGFloat, x2a:CGFloat, y2a:CGFloat, x3a:CGFloat, y3a:CGFloat, x4a:CGFloat, y4a:CGFloat) -> CATransform3D {
let y21 = y2a - y1a
let y32 = y3a - y2a
let y43 = y4a - y3a
let y14 = y1a - y4a
let y31 = y3a - y1a
let y42 = y4a - y2a
let a = -H*(x2a*x3a*y14 + x2a*x4a*y31 - x1a*x4a*y32 + x1a*x3a*y42)
let b = W*(x2a*x3a*y14 + x3a*x4a*y21 + x1a*x4a*y32 + x1a*x2a*y43)
let c0 = -H*W*x1a*(x4a*y32 - x3a*y42 + x2a*y43)
let cx = H*X*(x2a*x3a*y14 + x2a*x4a*y31 - x1a*x4a*y32 + x1a*x3a*y42)
let cy = -W*Y*(x2a*x3a*y14 + x3a*x4a*y21 + x1a*x4a*y32 + x1a*x2a*y43)
let c = c0 + cx + cy
let d = H*(-x4a*y21*y3a + x2a*y1a*y43 - x1a*y2a*y43 - x3a*y1a*y4a + x3a*y2a*y4a)
let e = W*(x4a*y2a*y31 - x3a*y1a*y42 - x2a*y31*y4a + x1a*y3a*y42)
let f0 = -W*H*(x4a*y1a*y32 - x3a*y1a*y42 + x2a*y1a*y43)
let fx = H*X*(x4a*y21*y3a - x2a*y1a*y43 - x3a*y21*y4a + x1a*y2a*y43)
let fy = -W*Y*(x4a*y2a*y31 - x3a*y1a*y42 - x2a*y31*y4a + x1a*y3a*y42)
let f = f0 + fx + fy;
let g = H*(x3a*y21 - x4a*y21 + (-x1a + x2a)*y43)
let h = W*(-x2a*y31 + x4a*y31 + (x1a - x3a)*y42)
let i0 = H*W*(x3a*y42 - x4a*y32 - x2a*y43)
let ix = H*X*(x4a*y21 - x3a*y21 + x1a*y43 - x2a*y43)
let iy = W*Y*(x2a*y31 - x4a*y31 - x1a*y42 + x3a*y42)
var i = i0 + ix + iy
let epsilon = CGFloat(0.0001);
if fabs(i) < epsilon {
i = epsilon * (i > 0 ? 1 : -1);
}
return CATransform3D(m11: a/i, m12: d/i, m13: 0, m14: g/i, m21: b/i, m22: e/i, m23: 0, m24: h/i, m31: 0, m32: 0, m33: 1, m34: 0, m41: c/i, m42: f/i, m43: 0, m44: 1.0)
}
有没有办法让红色矩形完全位于所有设备的绿色部分? 或者如果我的方法有误,请建议我正确的。
谢谢。