UIView使用少量对象制作动画

时间:2018-05-20 07:59:23

标签: 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)
                }

2 个答案:

答案 0 :(得分:0)

根据代码,它们都在同一时间执行(延迟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

答案 1 :(得分:0)

只是增加延迟时间,因为循环很快而且没有花时间,每个元素同时都在说明,所以你需要为每个动画启动不同的延迟。创建一个变量animationDelay并将其传递给uiview.animaton

var animationDelay = 1.0 //your dealy for first element


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
      //set created animationDelay 
      UIView.animate(withDuration: 1.0, delay: animationDelay, options: [], animations: {
          button.transform = CGAffineTransform(translationX: cosX, y: sinY).concatenating(CGAffineTransform(scaleX: 0.5, y: 0.5))
          button.alpha = 1
      }) { (done) in
          print(done)
      }
      animationDelay += 0.35 // increment the delay you want between each elemnts 
}