2017-08-22 71 views
1

我需要添加到我的視圖n個子視圖的時間間隔,並設置它們的角半徑也與時間間隔。 問題是,他們都一起調用自我沒有時間間隔(立即)。如何以時間間隔動畫調用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) 
     } 
    } 
} 
+0

viewWillAppear中後,你爲什麼打電話viewWillLayoutSubviews? – Tj3n

+0

我有一個標籤欄,我需要我重繪子視圖,如果我通過標籤 –

+0

你搞亂了VC委託方法,我不認爲它是一個好主意,只是用'view.layoutIfNeeded()'調用你的動畫,而不是調用請再次委託方法 – Tj3n

回答

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

它不適合我。我把它寫在「for i in 1 ... count」循環中,但是一切仍然是 –

+0

請記住設置一個起始狀態,然後在動畫塊中放置結束狀態(我將用對此效果的註釋更新答案)約束是可以動畫的,iirc,但是他們可能會遇到一些問題。除了創建方法外,您可能需要引用您的視圖。 – solenoid

相關問題