更改UIView的锚点将移动视图

问题描述

我希望能够在X / Y中心以外的其他锚点上旋转视图。我搜索一个解决方案,并找到了以下扩展名。但是,在我看来,它仍然可以移动我更改了锚点的视图。请查看下面的屏幕截图。

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

然后将以下行添加到ViewDidLoad。

@R_982_4045@ionBarButtonViewLinetop?.setAnchorPoint(CGPoint(x: 1,y: 0.5))
@R_982_4045@ionBarButtonViewLineBottom?.setAnchorPoint(CGPoint(x: 1,y: 0.5))

以下是屏幕截图:

enter image description here

顶部和底部的绿线是灰色视图的子视图。我想更改子视图(绿线视图)的锚点,这些子视图的名称分别为@R_982_4045@ionBarButtonViewLinetop和@R_982_4045@ionBarButtonViewLineBottom。

我使用StoryBoard设置自动布局,以在灰色框中显示绿线,如下所示。

enter image description here

预先感谢您的想法。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)