iOS

当前位置:首页 > iOS

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

我有一个自定义UIView,我想动画其backgroundColor属性.这是UIView的 animatable property. 这是代码: class ETTimerUI...
我有一个自定义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`所遇到的程序开发问题。

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

相关内容

文章评论

表情

共 0 条评论,查看全部
  • 这篇文章还没有收到评论,赶紧来抢沙发吧~

评论排行榜

热门标签