ios – 使用标签栏关闭视图控制器后,动画不停止

前端之家收集整理的这篇文章主要介绍了ios – 使用标签栏关闭视图控制器后,动画不停止前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
问题
我有两个视图控制器,都包含在相应的UINavigationController和一个UITabBarController内.在其中一个视图控制器中,我正在创建一个气泡效果,我在屏幕上画出气泡并对其位置进行动画处理.当我使用标签栏移动到另一个视图控制器时,会出现此问题,这会导致cpu飙升并保持在100%,气泡继续动画.


气泡的代码封装在UIView子类中.

  1. override func draw(_ rect: CGRect) {
  2. // spawn shapes
  3. for _ in 1 ... 10 { // spawn 75 shapes initially
  4. spawn()
  5. }
  6. }

drawRect方法重复地调用spawn函数以使用气泡填充视图.

  1. fileprivate func spawn() {
  2. let shape = CAShapeLayer()
  3. shape.opacity = 0.0
  4.  
  5. // create an inital path at the starting position
  6. shape.path = UIBezierPath(arcCenter: CGPoint.zero,radius: 1,startAngle: 0,endAngle: 360 * (CGFloat.pi / 180),clockwise: true).cgPath
  7. shape.position = CGPoint.zero
  8.  
  9. layer.addSublayer(shape)
  10.  
  11.  
  12. // add animation group
  13. CATransaction.begin()
  14.  
  15. let radiusAnimation = CABasicAnimation(keyPath: "path")
  16. radiusAnimation.fromValue = shape.path
  17. radiusAnimation.toValue = UIBezierPath(arcCenter: center,radius: 100,clockwise: true).cgPath
  18.  
  19. CATransaction.setCompletionBlock { [unowned self] in
  20.  
  21. // remove the shape
  22. shape.removeFromSuperlayer()
  23. shape.removeAllAnimations()
  24.  
  25. // spawn a new shape
  26. self.spawn()
  27. }
  28.  
  29. let movementAnimation = CABasicAnimation(keyPath: "position")
  30. movementAnimation.fromValue = NSValue(cgPoint: CGPoint.zero)
  31. movementAnimation.toValue = NSValue(cgPoint: CGPoint(x: 100,y: 100))
  32.  
  33.  
  34. let animationGroup = CAAnimationGroup()
  35. animationGroup.animations = [radiusAnimation,movementAnimation]
  36. animationGroup.fillMode = kCAFillModeForwards
  37. animationGroup.isRemovedOnCompletion = false
  38. animationGroup.duration = 2.0
  39.  
  40. shape.add(animationGroup,forKey: "bubble_spawn")
  41.  
  42. CATransaction.commit()
  43. }

在CATransaction完成处理程序中,我从超级视图中删除形状并创建一个新的形状.对self.spawn()的函数调用似乎是问题

在viewDidDisappear的包含视图控制器我呼叫以下:

  1. func removeAllAnimationsFromLayer() {
  2.  
  3. layer.sublayers?.forEach({ (layer) in
  4. layer.removeAllAnimations()
  5. layer.removeFromSuperlayer()
  6. })
  7.  
  8. CATransaction.setCompletionBlock(nil)
  9. }

尝试从答案
我试图将removeAllAnimations函数添加到UITabBarControllerDelegate

  1. extension BaseViewController: UITabBarControllerDelegate {
  2.  
  3. func tabBarController(_ tabBarController: UITabBarController,didSelect viewController: UIViewController) {
  4.  
  5. bubblesView.removeAllAnimationsFromLayer()
  6. }
  7. }

解决方法

我想你的问题是,你只使用一个线程的所有东西.请随时随地发送影响您的GUI的所有内容,并将其显示给其他线程的新的spawn实例.看看怎么回事这样的事情
  1. fileprivate func spawn() {
  2.  
  3. let shape = CAShapeLayer()
  4. shape.opacity = 0.0
  5.  
  6. // create an inital path at the starting position
  7. shape.path = UIBezierPath(arcCenter: CGPoint.zero,clockwise: true).cgPath
  8. shape.position = CGPoint.zero
  9.  
  10.  
  11. // create an inital path at the starting position
  12. shape.path = UIBezierPath(arcCenter: startingPosition,radius: startRadius,startAngle: BubbleConstants.StartingAngle,endAngle: BubbleConstants.EndAngle,clockwise: true).cgPath
  13. shape.position = startingPosition
  14.  
  15. // set the fill color
  16. shape.fillColor = UIColor.white.cgColor
  17.  
  18. layer.addSublayer(shape)
  19.  
  20. shape.opacity = Float(opacity)
  21.  
  22. DispatchQueue.main.async {
  23. self.layer.addSublayer(shape)
  24. CATransaction.begin()
  25. }
  26.  
  27. let radiusAnimation = CABasicAnimation(keyPath: "path")
  28. radiusAnimation.fromValue = shape.path
  29. radiusAnimation.toValue = UIBezierPath(arcCenter: center,radius: endRadius,clockwise: true).cgPath
  30.  
  31.  
  32. DispatchQueue.main.async { [unowned self] in
  33. CATransaction.setCompletionBlock { [unowned self] in
  34.  
  35. // remove the shape
  36. DispatchQueue.main.async {
  37. shape.removeFromSuperlayer()
  38. shape.removeAllAnimations()
  39. }
  40.  
  41. DispatchQueue.global(qos: .background).async {
  42. // spawn a new shape
  43. self.spawn()
  44. }
  45. }
  46. }
  47.  
  48.  
  49. let movementAnimation = CABasicAnimation(keyPath: "position")
  50. movementAnimation.fromValue = NSValue(cgPoint: startingPosition)
  51. movementAnimation.toValue = NSValue(cgPoint: destination)
  52.  
  53.  
  54. let animationGroup = CustomAnimationGroup()
  55. animationGroup.animations = [radiusAnimation,movementAnimation]
  56. animationGroup.fillMode = kCAFillModeForwards
  57. animationGroup.isRemovedOnCompletion = false
  58. animationGroup.duration = duration
  59.  
  60. shape.add(animationGroup,forKey: "bubble_spawn")
  61.  
  62. DispatchQueue.main.async {
  63. CATransaction.commit()
  64. }
  65. }

猜你在找的iOS相关文章