子视图背景颜色不动画

时间:2017-02-20 06:24:55

标签: ios swift uiview uianimation

我有一个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会立即变为深灰色......

3 个答案:

答案 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

进行设置

答案 1 :(得分: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)

答案 2 :(得分:0)

因此,由于drawRect的实现,无法设置波形视图背景颜色。我要做的是将背景颜色设置为波形视图以清除,然后在其后面添加另一个UIView并为该视图的背景颜色设置动画。