【问题标题】:How to animate invoking of n subviews with time interval如何以时间间隔动画调用 n 个子视图
【发布时间】:2018-01-30 05:13:21
【问题描述】:

我需要将 n 个具有时间间隔的子视图添加到我的视图中,然后将它们设置为具有时间间隔的角半径。 问题是它们一起调用 self 而没有时间间隔(立即)。

我尝试了 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)
        }
    }
}

【问题讨论】:

  • 为什么在 viewWillAppear 之后调用 viewWillLayoutSubviews?
  • 我有一个标签栏,如果我浏览标签,我需要重绘子视图
  • 你在搞乱vc委托方法,我觉得这不是一个好主意,用view.layoutIfNeeded()调用你的动画,不要再调用委托方法
  • 能否请您添加您打算拥有的视图的屏幕截图?

标签: ios swift animation view


【解决方案1】:

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

    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
        })
    }

【讨论】:

  • 它对我不起作用。我把它写在“for i in 1...count”循环中,但一切都还在
  • 记得设置一个开始状态,然后在动画块中放置结束状态(我会用评论更新答案)约束是可动画的,iirc,但可能会有一些问题他们。您可能需要在创建它们的方法之外引用您的视图。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多