iPhone shine animation

前端 未结 5 1885
北海茫月
北海茫月 2021-02-01 10:53

I\'m trying to get my view to do a nice shining animation to catch the user\'s eyes. Any ideas how to implement this?

Here\'s what I have so far:

[UIView         


        
5条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-01 11:05

    Here is the swift code for the shine effect

    class Animate {
    
        /// Add a persistent shimmer animation. Usage: `Animate.shimmer(myView)`
        static func shimmer(view: UIView) {
          let gradient = CAGradientLayer()
          gradient.startPoint = CGPointMake(0, 0)
          gradient.endPoint = CGPointMake(1, -0.02)
          gradient.frame = CGRectMake(0, 0, view.bounds.size.width*3, view.bounds.size.height)
    
          let lowerAlpha: CGFloat = 0.7
          let solid = UIColor(white: 1, alpha: 1).CGColor
          let clear = UIColor(white: 1, alpha: lowerAlpha).CGColor
          gradient.colors     = [ solid, solid, clear, clear, solid, solid ]
          gradient.locations  = [ 0,     0.3,   0.45,  0.55,  0.7,   1     ]
    
          let theAnimation : CABasicAnimation = CABasicAnimation(keyPath: "transform.translation.x")
          theAnimation.duration = 4
          theAnimation.repeatCount = Float.infinity
          theAnimation.autoreverses = false
          theAnimation.removedOnCompletion = false
          theAnimation.fillMode = kCAFillModeForwards
          theAnimation.fromValue = -view.frame.size.width * 2
          theAnimation.toValue =  0
          gradient.addAnimation(theAnimation, forKey: "animateLayer")
    
          view.layer.mask = gradient
    
        }
    }
    

    Swift 3.0

    func shimmer(view: UIView) {
            let gradient = CAGradientLayer()
            gradient.startPoint = CGPoint(x: 0, y: 0)
            gradient.endPoint = CGPoint(x: 1, y: -0.02)
            gradient.frame = CGRect(x: 0, y: 0, width: view.bounds.size.width*3, height: view.bounds.size.height)
    
            let lowerAlpha: CGFloat = 0.7
            let solid = UIColor(white: 1, alpha: 1).cgColor
            let clear = UIColor(white: 1, alpha: lowerAlpha).cgColor
            gradient.colors     = [ solid, solid, clear, clear, solid, solid ]
            gradient.locations  = [ 0,     0.3,   0.45,  0.55,  0.7,   1     ]
    
            let theAnimation : CABasicAnimation = CABasicAnimation(keyPath: "transform.translation.x")
            theAnimation.duration = 2
            theAnimation.repeatCount = Float.infinity
            theAnimation.autoreverses = false
            theAnimation.isRemovedOnCompletion = false
            theAnimation.fillMode = kCAFillModeForwards
            theAnimation.fromValue = -view.frame.size.width * 2
            theAnimation.toValue =  0
            gradient.add(theAnimation, forKey: "animateLayer")
    
            view.layer.mask = gradient
        }
    

提交回复
热议问题