代码之家  ›  专栏  ›  技术社区  ›  Sandero

在我的情况下,你可以改变星星和闪烁停止动画?

  •  0
  • Sandero  · 技术社区  · 6 年前

    我有这样一个想这样做的情况下刷新后几秒钟都加载了微光和完成动画。 我的代码starAnimatiom:

    func startAnimation() {
            for animateView in getSubViewsForAnimate() {
                animateView.clipsToBounds = true
                let gradientLayer = CAGradientLayer()
                gradientLayer.colors = [UIColor.clear.cgColor, UIColor.white.withAlphaComponent(0.8).cgColor, UIColor.clear.cgColor]
                gradientLayer.startPoint = CGPoint(x: 0.7, y: 1.0)
                gradientLayer.endPoint = CGPoint(x: 0.0, y: 0.8)
                gradientLayer.frame = animateView.bounds
                animateView.layer.mask = gradientLayer
    
                let animation = CABasicAnimation(keyPath: "transform.translation.x")
                animation.duration = 1.5
                animation.fromValue = -animateView.frame.size.width
                animation.toValue = animateView.frame.size.width
                animation.repeatCount = .infinity
    
                gradientLayer.add(animation, forKey: "")
            }
        }
        func getSubViewsForAnimate() -> [UIView] {
            var obj: [UIView] = []
            for objView in view.subviewsRecursive() {
                obj.append(objView)
            }
            return obj.filter({ (obj) -> Bool in
                obj.shimmerAnimation
    
            })
        }
    

    我的代码函数停止动画;

    @objc func stopAnimation() {
            for animateView in getSubViewsForAnimate() {
                animateView.layer.removeAllAnimations()
                animateView.layer.mask = nil
                timerShimmer.invalidate()
                refresh.endRefreshing()
    
    
            }
    
    
        }
    

    当我下拉并进行更新时,动画仍在继续,但由于某些原因没有停。什么我做错了吗?

     @objc func obnova() {
    
    
            self.startAnimation()
            self.tableView.reloadData()
    
            self.loadObjects1()
            self.loadObjects2()
            self.loadObjects3()
    
          // self.refresh.endRefreshing()
    
        }
    
    
    
    override func viewDidLoad() {
            super.viewDidLoad()
    
     timerShimmer = Timer.init(timeInterval: 0.2, target: self, selector: #selector(stopAnimation), userInfo: nil, repeats: true)
    }
    

    请帮帮我?

    0 回复  |  直到 6 年前
        1
  •  0
  •   Cerlin    6 年前

    像下面这样更改最后一个部分并尝试

    func startTimer() {
        if timerShimmer != nil {
            timerShimmer.invalidate()
            timerShimmer = nil
        }
    
        timerShimmer = Timer.init(timeInterval: 0.2, target: self, selector: #selector(stopAnimation), userInfo: nil, repeats: true)
    }
    
    @objc func obnova() {
        self.startAnimation()
        self.tableView.reloadData()
    
        self.loadObjects1()
        self.loadObjects2()
        self.loadObjects3()
        startTimer()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        startTimer()
    }
    
    推荐文章