问题描述
在 UIDatePicker 控件中更改年份时,向右箭头图像中使用的动画过渡到向下箭头图像是什么?动画平滑地旋转起始图像到结束图像,如下所示:
我正在尝试在我的代码中模拟图像的这种转换。在 viewDidLoad() 中,我有:
myButton.setImage(UIImage(named: "rightArrow")?.withRenderingMode(.alwaysTemplate),for: .normal)
然后在点击该按钮时调用的函数中,我这样做:
UIView.transition(with: myButton,duration: 0.6,options: .transitionCrossDissolve,animations: {
self.myButton.setImage(UIImage(named: "downArrow")?.withRenderingMode(.alwaysTemplate),for: .normal)
},completion: nil)
我在这里使用 .transitionCrossDissolve
,但显然这不是正确的过渡。我尝试了七个可用的过渡选项中的每一个,但它们的行为都不像 UIDatePicker 中的那样。有没有一种简单的方法来创建这种“旋转”过渡?
解决方法
我终于明白了。这将为箭头的旋转设置动画:
UIView.transition(with: myButton,duration: 0.2,options: [],animations: {
self.myButton.transform = CGAffineTransform(rotationAngle: .pi/2)
},completion: nil)
编辑——更短的解决方案:
UIView.animate(withDuration: 0.2) {
self.myButton.transform = CGAffineTransform(rotationAngle: .pi/2)
}