В моем случае, вы можете пометить и изменить анимацию остановки мерцания? - PullRequest
0 голосов
/ 12 июня 2019

У меня есть случай такого желания сделать, чтобы после обновления пару секунд были загружены мерцанием и закончена анимация. мой код 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

        })
    }

Мой код функции stopAnimation;

@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)
}

Помогите, пожалуйста?

1 Ответ

0 голосов
/ 12 июня 2019

Измените последний раздел, как показано ниже, и попробуйте

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()
}
...