NIB中CGAffineTransform面临的问题

时间:2019-02-11 06:13:15

标签: ios xib swift4.2

我有2个视图(viewLeftviewRight)。我通过计算相同的锚点,使用天使0(初始设置)旋转这些视图,以使视图看起来像跨铰链旋转。

我已经完成了下面的代码,如果我在情节提要(viewcontroller)中获取视图,则工作正常,如果我将相同的视图和代码复制并粘贴到笔尖中,则无法正常工作。在《鸟巢》中,天使视角为0时,它们之间留有一些空间。这是奇怪的行为,我检查了所有代码输出和约束,一切都很好,但无法弄清为什么它不起作用。

我希望它可以在NIB中工作。

任何帮助都是有意义的。

Github 虚拟项目参考here

输出:

从情节提要

enter image description here

来自XIB

enter image description here

代码工作

设置锚点

extension UIView {
    func setAnchorPoint(_ point: CGPoint) {
        var newPoint = CGPoint(x: bounds.size.width * point.x, y: bounds.size.height * point.y)
        var oldPoint = CGPoint(x: bounds.size.width * layer.anchorPoint.x, y: bounds.size.height * layer.anchorPoint.y);

        newPoint = newPoint.applying(transform)
        oldPoint = oldPoint.applying(transform)

        var position = layer.position

        position.x -= oldPoint.x
        position.x += newPoint.x

        position.y -= oldPoint.y
        position.y += newPoint.y

        layer.position = position
        layer.anchorPoint = point
    }
}

旋转功能

func rotateView(_ addAngel : CGFloat) {

    let rotateToAngel = angel + addAngel

    guard rotateToAngel <= 0.6 && rotateToAngel >= -0.6  else {
        return
    }
    angel = rotateToAngel
    let anchorPointLeft = CGPoint(x: (viewLeft.frame.width - viewLeft.frame.height/2)/viewLeft.frame.width,
                                  y: ( viewLeft.frame.height/2)/viewLeft.frame.height)

    viewLeft.setAnchorPoint(anchorPointLeft)

    let anchorPointRight = CGPoint(x: (viewRight.frame.height/2/viewRight.frame.width),
                                   y: ( viewRight.frame.height/2)/viewRight.frame.height)

    viewRight.setAnchorPoint(anchorPointRight)


    self.viewLeft.transform = CGAffineTransform.init(rotationAngle: rotateToAngel)
    self.viewRight.transform = CGAffineTransform.init(rotationAngle: -rotateToAngel)

}

0 个答案:

没有答案