【问题标题】:subview background color not animating子视图背景颜色没有动画
【发布时间】:2017-02-20 06:24:54
【问题描述】:

我有一个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 在其他两个视图完成动画后立即变为深灰色...

【问题讨论】:

  • 无法从上面的代码块中得到一个想法,请您与 arunjos007@gmail.com 分享您的项目,然后我会尝试找出问题
  • 朋友你在设置初始颜色吗?
  • 我已经尝试过您的代码,它运行良好。所有三种颜色都在动画中......
  • @Mukesh 在尝试将其设置为灰色之前,我不会在任何地方设置背景颜色。它在故事板中的默认白色
  • 我已经更新了答案,那段代码总是为我工作。

标签: ios swift uiview uianimation


【解决方案1】:

我做动画的想法是这样的

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 &lt;QuartzCore/QuartzCore.h&gt;

斯威夫特

//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

【讨论】:

  • 我的子视图不仅仅是我通过界面生成器添加的 UIview。它是定制的和以编程方式创建的。
  • 你能用.layer试试吗?
  • 使用.layer,视图根本不会改变颜色。
  • 很奇怪..它也在我的项目中工作。我认为您在不同的地方做错了什么,发送您的项目 mukesh.lokare1@gmail.com 我会尝试找出问题。
【解决方案2】:

试试这个:-

   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)

【讨论】:

  • 好吧这种作品。我做了一些调整。我同样离开了with: self.view。在动画下,我正在更改self.viewself.waveformself.navigationController?.navigationBar 的背景。我没有使用.layer。现在导航栏和波形视图同步动画,但在波形和导航栏都已动画为灰色后,主视图立即变为灰色。
  • 你的主视图已经是灰色的了吗?尝试在动画之前设置不同的(可能是白色)颜色,然后将背景颜色设置为灰色。增加动画时间并在 viewDidAppear() 中做所有这些事情
  • 没有主视图已经是白色的了。它立即变为灰色,而其他视图则变为动画。做ViewDidAppear()中的动画吗?为什么?这一切都发生在按下按钮之后。
  • 好的,那么请更新代码你在方法中做什么?
  • @Brosef 查看我更新的答案并将您的视图代码放入其中。它在测试项目中为我工作。
【解决方案3】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-06
    • 1970-01-01
    • 2018-10-13
    • 2012-12-18
    • 2023-03-11
    • 2010-09-16
    相关资源
    最近更新 更多