2017-02-20 133 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并为该视图的背景颜色设置动画。