我试图在按钮中创建动画旋转图像,但图像在动画之前跳跃,即旋转 180 度并开始动画
我的代码:
@IBAction func switchListButtonClick(sender: UIButton) {
UIView.animateWithDuration(1) {
self.switchListButton.imageView?.transform = CGAffineTransformRotate((self.switchListButton.imageView?.transform)!, CGFloat(-M_PI_2))
}
}
我真的不明白为什么会发生这种情况?
请帮助我。
最佳答案
这对我有用
self.switchListButton.imageView!.clipsToBounds = false;
self.switchListButton.imageView!.contentMode = .Center;
UIView.animateWithDuration(1) {
self.switchListButton.imageView?.transform = CGAffineTransformRotate((self.switchListButton.imageView?.transform)!, CGFloat(-M_PI_2))
}
虽然不完全确定,但我想下图可以说明上面的代码为何有效。您可以看到,当您旋转 UIView 时, View 的边界可能会改变并超出其父 View (下图中红线上方),因此如果您将 clipToBounds
设置为 true,则转换可能会失败.
关于ios - CGAffineTransform Rotate - 动画前跳转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37681920/