如何使用时间间隔动画调用n个子视图

时间:2017-08-22 04:11:16

标签: ios swift animation view

我需要在时间间隔添加到我的视图n子视图中,并且还要按时间间隔设置角半径。 问题是他们一起调用自己没有时间间隔(立即)。

我尝试了Timer.sheduledTimer和GCD,但我没有结果。 也许我做错了什么 请告诉我们该做什么

func addSubviews(count : Int, completeon : (_ view : UIView)->()) {
    view.subviews.forEach({ $0.removeFromSuperview() })
    for i in 1...count {
        let inscribedView = UIView()
        inscribedView.translatesAutoresizingMaskIntoConstraints = false
        let sizeConstant = ((self.screenWidth / 2 ) / self.viewsCount )
            self.view.addSubview(inscribedView)
        //setup constraints
        self.view.addConstraint(NSLayoutConstraint.init(item: inscribedView, attribute: .top, relatedBy: .equal, toItem: self.view, attribute: .top, multiplier: 1, constant: CGFloat(count == viewsCount ? statusBarHeight + sizeConstant * i : sizeConstant * i)))
        self.view.addConstraint(NSLayoutConstraint.init(item: inscribedView, attribute: .right, relatedBy: .equal, toItem: self.view, attribute: .right, multiplier: 1, constant: -CGFloat(sizeConstant * i)))
        self.view.addConstraint(NSLayoutConstraint.init(item: inscribedView, attribute: .bottom, relatedBy: .equal, toItem: self.view, attribute: .bottom, multiplier: 1, constant: -(CGFloat)(count == self.viewsCount ? self.tabBarHeight + sizeConstant * i : sizeConstant * i)))
        self.view.addConstraint(NSLayoutConstraint.init(item: inscribedView, attribute: .left, relatedBy: .equal, toItem: self.view, attribute: .left, multiplier: 1, constant: CGFloat(sizeConstant * i)))

        inscribedView.backgroundColor = RandomFlatColor()
    }

       completeon(view)
}

override func viewDidLoad() {
    super.viewDidLoad()
    self.becomeFirstResponder()


}
override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    viewWillLayoutSubviews()
}
override func viewWillLayoutSubviews() {
    addSubviews(count: viewsCount) { view in
        for subview in view.subviews {
                subview.addCornerRadiusAnimation(from: 0, to: 20, duration: 0.3)
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我使用以下内容在一组具有不同时序的视图中制作动画:

    let views:[UIView] = [view00,view01, view02]
    let timing:[Double] = [0.806,0.816,0.826]
    for (index,view) in views.enumerated() {
        //set any view properties that will be animated to starting state
        view.alpha = 0
        // animate each view
        UIView.animate(withDuration: 0.28, delay: timing[index], options: .curveEaseOut, animations:  {() -> Void in
        //set the final view properties that are animateable here
            view.alpha = 1
        }, completion: {(finished:Bool) -> Void in
            //can set off a second animation here
        })
    }