几次旋转后,CGAffineTransform会破坏UIBarButton

时间:2017-02-17 23:25:14

标签: ios swift

有没有人在尝试使用CGAffineTransform旋转几次对称形状的UIBarButton动画时出现过几次情况,然后按钮变形了

optionButton.transform = CGAffineTransform(rotationAngle: CGFloat(M_PI))

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:1)

通过将UIButton的身份矩阵重置为身份,似乎可以解决问题。但是为什么呢???

UIView.animate(withDuration: 0.20, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: UIViewAnimationOptions.curveEaseIn, animations: { 
            if let optionsButton = self.homeController?.optionsButton {
                optionsButton.transform = optionsButton.transform.rotated(by: CGFloat(-M_PI_2))
            }
        }) { (finished: Bool) in
            if let optionsButton = self.homeController?.optionsButton {
                optionsButton.transform = CGAffineTransform.identity
            }
}

好的,正确的&简短的回答是我忘了定义" contentMode"我的UIButton的backgroundImage。

button.contentMode = .scaleAspectFit

如果我错了,请纠正我。答案很长:

UIButton的UIView图层引用的单位矩阵图层不是方形(如果只有几个像素差异,可能很难发现形状上的差异。查看Xcode' UI调试工具可以提供帮助。)当按钮为自己设置动画时,较长边与标识矩阵层(不是UIView)中的较短边交换位置。因此,如果我继续点击这个UIButton用于动画,那么"矩形"会长得更久,然后最终变得紧张。重新设置UIButton的单位矩阵可以偶然解决问题,因为矩阵层重置回原来的位置,但它只是一个hacky解决方案。

相关问题