CATransform3dTranslate issue in imageView

我是研究僧i 提交于 2019-12-25 16:01:32

问题


i have a problem with CATransform3DTranslate. i concatenating it with CATransform3D with the code from KennyTM here

i measured the coordinate using Photoshop. here is my complete code :

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)

and that code is running perfectly on iPhone 4s - iPhone 5s. but when i use iPhone 6 or 6 Plus simulator, it's not looking right. here is the screenshot (the red rectangle has to be exactly in the green section). and here without CATransform3DTranslate

here is the code for 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)      
    }

is there any way to make the red rectangle to be exactly in the green section in all device? or if my approach is wrong, please suggest me the right one.

thank you.

来源:https://stackoverflow.com/questions/27934454/catransform3dtranslate-issue-in-imageview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!