Facing Issue with CGAffineTransform in NIB

梦想的初衷 提交于 2019-12-11 02:20:00

问题


I have 2 Views (viewLeft, viewRight). I am rotating these views with angel 0 (initial setup) by calculating the same anchor point so that it looks like views are rotating across hinge.

I have done below code, which is working fine if I took the views in storyboard (viewcontroller) and it does not work if I copy and paste same views and code in nib. In Nib, at 0 angel views leaves some space between them. It is weird behavior, I checked all code outlets and constraints, everything is fine but unable to figure out why it is not working.

I want it to be working in NIB.

Any help will be appreciable.

Github dummy project reference here

Outputs :

From Storyboard

From XIB

Code work

Set Anchor point

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
    }
}

Rotation function

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)

}

来源:https://stackoverflow.com/questions/54624858/facing-issue-with-cgaffinetransform-in-nib

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