将一个动画隔开一秒钟的最佳方法是什么?

时间:2018-02-16 12:01:31

标签: ios swift grand-central-dispatch

除了使用public function add_admin() { $this->data['main_content'] = 'admin/admin_config/index'; $this->data['sub_content'] = 'admin/admin_config/admin_form'; $this->load->view(BACKEND, $this->data); } public function store_admin() { $post = $this->input->post(); unset($post['designation'], $post['password'], $post['re_pass']); $post['password'] = sha1($this->input->post('password')); $post['designation'] = implode(', ', $this->input->post('designation')); $c_pass = sha1($this->input->post('re_pass')); $image_name = $_FILES['admin_image']; $post['filename'] = $image_name['name']; $config = [ 'upload_path' => './upload/admin/', 'allowed_types' => 'jpg|jpeg|png', ]; $this->load->library('upload', $config); if ($post) { if ($post['password'] == $c_pass && $this->upload->do_upload('admin_image')) { $this->adminconfig_model->insert($post); set_flash('msg', 'Admin added Successfully!!'); redirect('admin/adminConfig'); }else{ $this->data['error_image'] = $this->upload->display_errors(); $this->add_admin(); } } else { set_flash('dmsg', 'Password do not match!!'); $this->add_admin(); } } 三次之外,有更好的方法将这些动画间隔一秒钟吗?

asyncAfter

3 个答案:

答案 0 :(得分:4)

您可以使用UIView.animate(withDuration:delay:...)

甚至可以定义一个函数如下:

func animateSequentially(withDurations durations: [TimeInterval], animations: [(() -> Void)]) {

    guard durations.count == animations.count else {
        return
    }

    for index in 0..<animations.count {

        // calculate delay
        let delay: TimeInterval = durations[0..<index].reduce(TimeInterval(0), { delay, duration in
            return delay + duration
        })

        // perform animation
        UIView.animate(withDuration: durations[index],
                       delay: delay,
                       options: [],
                       animations: animations[index],
                       completion: nil)
    }
}

并像这样使用:

let myView = UIView()

let durations = [1, 0.5, 1.25]

let animations = [{ myView.backgroundColor = .red },
                  { myView.backgroundColor = .yellow },
                  { myView.backgroundColor = .blue }]

animateSequentially(withDurations: durations, animations: animations)

答案 1 :(得分:1)

你可以像这样嵌套/延迟

UIView.animate(withDuration: 0.25, delay: 0.0, options: [], animations: {
      // first animation
}, completion: { (finished: Bool) in

          UIView.animate(withDuration: 0.25, delay: 0.0, options: [], animations: {
            // second animation
          }, completion: { (finished: Bool) in


         })
})

答案 2 :(得分:0)

你可以创建任何你想要的工具,但你所拥有的东西看起来已经很完美了。这样看吧;完美的情况是:

ToolName.performAt(time, { execution })

因此,在您的情况下,ToolName可以替换为let queue = DispatchQueue.main,并且已完成。

但你肯定可以这样做:

    typealias ExecutionClosureType = () -> Void

    func executeInIntervals(intervalDuration: TimeInterval, toExecute closureArray: [ExecutionClosureType]) {
        let now = DispatchTime.now()
        let queue = DispatchQueue.main
        closureArray.enumerated().forEach { (index, closure) in
            queue.asyncAfter(deadline: now + intervalDuration*TimeInterval(index), execute: closure)
        }
    }

然后使用:

    executeInIntervals(intervalDuration: 1.0, toExecute: [
        {
            self.firstAnimation()
        }, 
        {
            self.secondAnimation()
        },
        {
            self.thirdAnimation()
        }
    ])

如果需要这个工具,你应该做这样的事情。