繁体   English   中英

从左到右滑动动画背景颜色 UIView 或 UIButton

[英]Slide animate background color left to right UIView or UIButton

如何从左向右滑动动画背景颜色 UIView 或 UIButton Swift (iOS)

动画示例:

 div { font: 22px Arial; display: inline-block; padding: 1em 2em; text-align: center; color: white; background: red; /* default color */ /* "to left" / "to right" - affects initial color */ background: linear-gradient(to left, salmon 50%, lightblue 50%) right; background-size: 200%; transition: .5s ease-out; } div:hover { background-position: left; }
 <div>Example</div>

class AnimatingButton: UIView {

func startProgressAnimation()
{
    let layer = CALayer()
    layer.backgroundColor = #colorLiteral(red: 0.4980392157, green: 0.8352941176, blue: 0.1921568627, alpha: 1)
    layer.masksToBounds = true
    layer.anchorPoint = .zero
    

    layer.frame = CGRect.init(x: 0,
                              y: 0,
                              width: self.frame.width,
                              height: self.frame.height)
    self.layer.addSublayer(layer)

    
    let boundsAnim:CABasicAnimation = CABasicAnimation(keyPath: "bounds.size.width")
    boundsAnim.fromValue = 0
    boundsAnim.toValue = self.frame.size.width
    let anim = CAAnimationGroup()
    anim.animations = [boundsAnim]
    anim.isRemovedOnCompletion = false
    anim.duration = 10
    
    layer.add(anim, forKey: "bounds.size.width")
}}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM