ios - UIView 使用少量对象进行动画处理

标签 ios swift uikit

我希望这些元素在持续时间内在一行中逐个制作动画,但它们是在同一时刻一起制作动画

            authClicked = true
            var angle: CGFloat = 45.0

            google = UIButton(type: .custom)
            facebook = UIButton(type: .custom)
            vk = UIButton(type: .custom)
            instagram = UIButton(type: .custom)
            regular = UIButton(type: .custom)

            let buttonArray = [self.google: "Google", self.facebook: "Facebook", self.vk: "VK", self.instagram: "Instagram", self.regular: "Regular"];

            for (button, buttonName) in buttonArray {
                button.frame = CGRect(x: x, y: y, width: 150, height: 150)
                button.addTarget(self, action: #selector(self.buttonClicked), for: .touchUpInside)
                button.setTitle(buttonName, for: .normal)
                button.alpha = 0

                circleButton(button: button)

                angle -= 45.0
                view.addSubview(button)

                let cosX = cos(angle) * 200
                let sinY = sin(angle) * 200

                UIView.animate(withDuration: 1.0, delay: 1.0, options: [], animations: {
                    button.transform = CGAffineTransform(translationX: cosX, y: sinY).concatenating(CGAffineTransform(scaleX: 0.5, y: 0.5))
                    button.alpha = 1
                }) { (done) in
                    print(done)
                }

最佳答案

根据代码,它们都是同时执行的(延迟1.0秒后)。

要更改此设置:

// create a variable outside of the for loop
var animationDelayTime = 1.0

// the for loop
for (button, buttonName) in buttonArray {

            // the other stuff
            ... 
            ...
            ...


            // instead of 1.0 for delay, use animationDelayTime
            UIView.animate(withDuration: 1.0, delay: animationDelayTime, options: [], animations: {
                button.transform = CGAffineTransform(translationX: cosX, y: sinY).concatenating(CGAffineTransform(scaleX: 0.5, y: 0.5))
                button.alpha = 1
            }) { (done) in
                print(done)
            }

    // after the animation is done, increment the var for the next iteration.
    // it will be 1.25 for the 2nd loop, then 1.50, then 1.75, etc.
    animationDelayTime += 0.25

} // end, for loop

关于ios - UIView 使用少量对象进行动画处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50432809/

相关文章:

ios - 开头的函数返回值

ios - 在 Playground 中使用实况照片

sql - SQLite 中的 Select 语句识别行号

ios - Alamofire+组合 : how to get custom error type out of AFError

ios - 如何使用 myapp 跟踪设备的位置

ios - 更新自定义单元格中的标签时自动换行不起作用(无需重新加载表格)

ios - 允许文本选择但不在 WkWebView 中显示上下文菜单

ios - 如何将加速框架与核心图形一起使用?

iOS:以 png 或 jpeg 格式写入 CGImageRef 磁盘

iphone - UIView animateWithDuration 不考虑延迟