我正在尝试为按钮设置动画,使其向左倾斜 pi/5,然后向右倾斜相同的量,以便它返回到其原始位置(我试图使按钮看起来好像在摇晃) )
但是,Xcode 剪掉了动画,因为它意识到按钮整体上并没有改变角度。这是当前的代码 - 如果有人有任何关于动画按钮来回摇动的建议,我将非常感激。
for i in 0...8 {
if pressedArray[i] == true {
self.panlButtons[i].backgroundColor = UIColor.clear
UIView.animate(withDuration: 1, animations:{
self.panlButtons[i].backgroundColor = self.wrongColour
self.panlButtons[i].transform = CGAffineTransform(rotationAngle: CGFloat.pi / -5)
})
UIView.animate(withDuration: 1, animations:{
self.panlButtons[i].backgroundColor = self.wrongColour
self.panlButtons[i].transform = CGAffineTransform(rotationAngle: CGFloat.pi / 5)
}, completion: { finished in
self.panlButtons[i].backgroundColor = self.notPressedColour
})
代码解释: PressedArray[] 是一个 bool 数组,指示按下了哪个按钮 panlButtons[] 是按钮数组 从0-8的for循环是因为只有8个按钮
最佳答案
在第一个动画完成后移动下一个动画,如下所示
UIView.animate(withDuration: 1, animations:{
self.panlButtons[i].backgroundColor = self.wrongColour
self.panlButtons[i].transform = CGAffineTransform(rotationAngle: CGFloat.pi / -5)
}, completion: {
UIView.animate(withDuration: 1, animations:{
self.panlButtons[i].backgroundColor = self.wrongColour
self.panlButtons[i].transform = CGAffineTransform(rotationAngle: CGFloat.pi / 5)
}, completion: { finished in
self.panlButtons[i].backgroundColor = self.notPressedColour
})
})
关于ios - Xcode 9 - 尝试为 UIView 设置动画两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50948525/