ios – 实现`drawRect`的`UIView`的动画`backgroundColor`

ios – 实现`drawRect`的`UIView`的动画`backgroundColor`,第1张

概述我有一个自定义UIView,我想动画其backgroundColor属性.这是UIView的 animatable property. 这是代码: class ETTimerUIView: UIView { required init(coder aDecoder: NSCoder) { super.init(coder: aDecoder) } // other method 我有一个自定义UIVIEw,我想动画其backgroundcolor属性.这是UIVIEw的 animatable property.

这是代码:

class ETTimerUIVIEw: UIVIEw {  required init(coder aDecoder: NSCoder) {    super.init(coder: aDecoder)  }  // other methods  func flashBg() {    UIVIEw.animateWithDuration( 1.0,animations: {      self.backgroundcolor = UIcolor.colorYellow()    })  }  overrIDe func drawRect() {    // Something related to a timer I'm rendering  }

此代码导致动画跳过并且颜色立即更改:

self.backgroundcolor = UIcolor.colorYellow() // Changes immediately to yellow

如果我为Alpha设置动画,则会按预期在1秒内激活1到0:

self.Alpha = 0 // animates

在这种情况下,如何设置背景颜色变化的动画?

> Implementing drawRect blocks backgroundColor animation,但尚未提供答案.
> Maybe this is why you can’t combine drawRect and animateWithDuration,但我不太懂.

我想我需要制作一个单独的视图 – 这应该放在同一个视图控制器的故事板中吗?以编程方式创建?

对不起,我是iOS和Swift的新手.

解决方法 当我尝试它时确实不起作用,我有一个相关的问题,即在动画中放置layoutIfNeeded()方法并使视图平滑地动画化( move button towards target using constraints,no reaction?).但在这种情况下,使用backgroundcolor,它不起作用.如果有人知道答案,我将有兴趣知道.

但是如果你现在需要一个解决方案,你可以创建一个仅用作容器的UIVIEw(以编程方式或通过故事板).然后在里面添加2个视图:一个在顶部,一个在下面,与容器具有相同的框架.而且您只需更改顶视图的Alpha,即可让用户看到后面的视图:

class MyVIEw : UIVIEw {    var top : UIVIEw!    overrIDe init(frame: CGRect) {        super.init(frame: frame)        self.backgroundcolor = UIcolor.bluecolor()        top = UIVIEw(frame: CGRectMake(0,self.frame.wIDth,self.frame.height))        top.backgroundcolor = UIcolor.yellowcolor()        self.addSubvIEw(top)    }    overrIDe func touchesBegan(touches: NSSet,withEvent event: UIEvent) {        let sub = UIVIEw(frame: CGRectMake(0,self.frame.height))        sub.backgroundcolor = UIcolor.purplecolor()        self.sendSubvIEwToBack(sub)        UIVIEw.animateWithDuration(1,animations: { () -> VoID in            self.top.Alpha = 0            }) { (success) -> VoID in                println("anim finished")        }    }}
总结

以上是内存溢出为你收集整理的ios – 实现`drawRect`的`UIView`的动画`backgroundColor`全部内容,希望文章能够帮你解决ios – 实现`drawRect`的`UIView`的动画`backgroundColor`所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1107610.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存