I have a case of such a want to do so after refresh a couple of seconds were loaded with shimmer and finished animation. my code 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
})
}
My code function stopAnimation;
@objc func stopAnimation() {
for animateView in getSubViewsForAnimate() {
animateView.layer.removeAllAnimations()
animateView.layer.mask = nil
timerShimmer.invalidate()
refresh.endRefreshing()
}
}
When I pull down and do the update the animation continues to act and for some reason does not stop.What did I do wrong?
@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)
}
Help me Please?
Change the last section like below and try
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()
}