ios - Xcode 9 - 尝试为 UIView 设置动画两次

标签 ios iphone swift xcode uiviewanimation

我正在尝试为按钮设置动画,使其向左倾斜 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/

相关文章:

ios - swift 2 : iOS 8+ How to store latitude/longitude with CoreLocation and stop locating

ios - 如何快速计算文件夹引用中的图像数量

iphone - 使用大量内存的功能

ios - 从另一个 UIViewController 的 UITableView 获取 UITableViewCell

数组中数组的 Swift 通用扩展

iphone - 通过域名屏蔽 UIWebView 中的图片

ios - Admob 插页式广告 - 无法展示插页式广告。还没准备好

iphone - 如何设置 iPhone 5 的边框尺寸

iphone - 如何本地化 reverseGeocodeLocation 的地址结果?

ios - NSURLSession 正确获取数据,但在 Task.resume() 之后不返回任何内容