2017-02-20 191 views
0

我有一個UIViewController包含呈現波形的子視圖。當按下按鈕時,我試圖讓主要UIView,子視圖和導航欄從白色變爲灰色。我已經能夠改變主視圖和導航欄,但無法弄清楚如何讓波形的子視圖改變。現在,子視圖立即變爲灰色,而不是與其他視圖和導航欄同步動畫。子視圖背景顏色不動畫

//View Controller Code 

UIView.animate(withDuration: 10.0, animations: { 

    self.navigationController?.navigationBar.barTintColor = UIColor(red: 20.00/255, green: 20.00/255, blue: 20.00/255, alpha: 1.00) 
    self.navigationController?.navigationBar.layoutIfNeeded() 
    self.waveform.backgroundColor = UIColor(red: 20.00/255, green: 20.00/255, blue: 20.00/255, alpha: 1.00) 
    self.view.backgroundColor = UIColor(red: 20.00/255, green: 20.00/255, blue: 20.00/255, alpha: 1.00) 

    }, completion: { finished in 

     //do stuff 
}) 

//waveform view 

override init(frame: CGRect) { 
    super.init(frame: frame) 
    self.setup() 
} 

required init?(coder aDecoder: NSCoder) { 
    super.init(coder: aDecoder) 
    self.setup() 
} 

override func draw(_ rect: CGRect) { 
    for i in 0..<self.circles.count { 
     self.draw(self.circles[i], ratio: -1.0) 
     self.draw(self.circles[i], ratio: 1.0) 
    } 
} 

func update(_ level: Double) { 

    amplitude = fmin(normalizedPowerLevelFromDecibels(level), maxAmplitude) 
    setNeedsDisplay() 
} 

//更新後的動畫

UIView.transition(with: self.view, duration: 1.0, options: [.transitionCrossDissolve], animations: { 

    self.view.backgroundColor = UIColor(red: 20.00/255, green: 20.00/255, blue: 20.00/255, alpha: 1.00) 


    self.navigationController?.navigationBar.barTintColor = UIColor(red: 20.00/255, green: 20.00/255, blue: 20.00/255, alpha: 1.00) 
    self.navigationController?.navigationBar.layoutIfNeeded() 

    self.waveform.backgroundColor = UIColor(red: 20.00/255, green: 20.00/255, blue: 20.00/255, alpha: 1.00) 

}, completion: nil) 

這會導致波形視圖和導航欄同步動畫,但self.view改變深灰色另外兩個意見完成後,動畫瞬間...

+0

無法從上面的代碼塊中獲得一個想法,請您與[email protected]分享您的項目,然後我會嘗試找出問題 – arunjos007

+0

朋友你是否設置了初始顏色? –

+0

我試過你的代碼,它工作正常。顏色爲所有三個動畫... –

回答

0

我做動畫的想法是這樣的

Objective-C的

//Set Initial frame or color here & then write block for animation 

[UIView animateWithDuration:1.0f animations:^{ 

     // Now set your changing animation frame or color here 

    }completion:^(BOOL finished) { 

     //Do completion stuff here 

    }]; 

如果你打算通過anyView.layer.backgroundColor然後#import <QuartzCore/QuartzCore.h>

斯威夫特

//Set Initial frame or color here & then write block for animation 

UIView.animate(withDuration: 5.0, animations: { 

     // Now set your changing animation frame or color here 
     self.view.layer.backgroundColor = UIColor(red: 20.00/255, green: 20.00/255, blue: 20.00/255, alpha: 1.00).cgColor 

}, completion: { finished in 

      //do completion stuffs 
    }) 

設置它。如果你要通過anyView.layer.backgroundColor設置它,然後import QuartzCore

+0

試過這個。它不幸的工作。 – Brosef

+0

我測試了你的代碼,它的工作, –

+0

我的子視圖不只是通過界面生成器添加的UIview。其定製和程序化創建。 – Brosef

0

試試這個在: -

UIView.animate(withDuration: 1.0, delay: 0.0, options:[.transitionCrossDissolve], animations: { 
     self.view1.backgroundColor = UIColor(red: 20.00/255, green: 20.00/255, blue: 20.00/255, alpha: 1.00) 
     self.view2.backgroundColor = UIColor(red: 20.00/255, green: 20.00/255, blue: 20.00/255, alpha: 1.00) 

     self.view.backgroundColor = UIColor(red: 26.00/255, green: 152.00/255, blue: 143.00/255, alpha: 1.00) 


     self.navigationController?.navigationBar.barTintColor = UIColor(red: 20.00/255, green: 20.00/255, blue: 20.00/255, alpha: 1.00) 
     self.navigationController?.navigationBar.layoutIfNeeded() 
    }, completion:nil) 
+0

好的這種作品。我做了一些調整。我用':self.view'離開'一樣。在動畫下,我改變了'self.view','self.waveform'和'self.navigationController?.navigationBar'的背景。我沒有使用'.layer'。現在導航欄和波形視圖同步生成動畫,但在波形和導航欄已經動畫變爲灰色後,主視圖立即變爲灰色。 – Brosef

+0

你的主視圖是灰色的嗎?嘗試在動畫之前設置不同的(可能是白色)顏色,然後將背景顏色設置爲灰色。增加動畫時間,並在viewDidAppear() – ChanWarde

+0

做所有這些東西否主視圖已經是白色的。它瞬間變成灰色,而其他視圖變成動畫。在'ViewDidAppear()'中做動畫嗎?爲什麼?這一切都發生在按下按鈕之後。 – Brosef

0

所以波形視圖背景顏色不能動畫,因爲我的實現drawRect。我必須做的是將背景顏色設置爲波形視圖以清除,然後在其後面添加另一個UIView併爲該視圖的背景顏色設置動畫。