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

问题
我有两个视图控制器,都包含在相应的UINavigationController和一个UITabBarController内.在其中一个视图控制器中,我正在创建一个气泡效果,我在屏幕上画出气泡并对其位置进行动画处理.当我使用标签栏移动到另一个视图控制器时,会出现此问题,这会导致cpu飙升并保持在100%,气泡继续动画.


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

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

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

fileprivate func spawn() {
    let shape = CAShapeLayer()
    shape.opacity = 0.0

    // create an inital path at the starting position
    shape.path = UIBezierPath(arcCenter: CGPoint.zero,radius: 1,startAngle: 0,endAngle: 360 * (CGFloat.pi / 180),clockwise: true).cgPath
    shape.position = CGPoint.zero

    layer.addSublayer(shape)


    // add animation group
    CATransaction.begin()

    let radiusAnimation = CABasicAnimation(keyPath: "path")
    radiusAnimation.fromValue = shape.path
    radiusAnimation.tovalue = UIBezierPath(arcCenter: center,radius: 100,clockwise: true).cgPath

    CATransaction.setCompletionBlock { [uNowned self] in

      // remove the shape
      shape.removeFromSuperlayer()
      shape.removeAllAnimations()

      // spawn a new shape
      self.spawn()
    }

    let movementAnimation = CABasicAnimation(keyPath: "position")
    movementAnimation.fromValue = NSValue(cgPoint: CGPoint.zero)
    movementAnimation.tovalue = NSValue(cgPoint: CGPoint(x: 100,y: 100))


    let animationGroup = CAAnimationGroup()
    animationGroup.animations = [radiusAnimation,movementAnimation]
    animationGroup.fillMode = kCAFillModeForwards
    animationGroup.isRemovedOnCompletion = false
    animationGroup.duration = 2.0

    shape.add(animationGroup,forKey: "bubble_spawn")

    CATransaction.commit()
  }

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

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

func removeAllAnimationsFromLayer() {

    layer.sublayers?.forEach({ (layer) in
      layer.removeAllAnimations()
      layer.removeFromSuperlayer()
    })

    CATransaction.setCompletionBlock(nil)
  }

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

extension BaseViewController: UITabBarControllerDelegate {

  func tabBarController(_ tabBarController: UITabBarController,didSelect viewController: UIViewController) {

    bubblesView.removeAllAnimationsFromLayer()
  }
}

解决方法

我想你的问题是,你只使用一个线程的所有东西.请随时随地发送影响您的GUI的所有内容,并将其显示给其他线程的新的spawn实例.看看怎么回事这样的事情
fileprivate func spawn() {

    let shape = CAShapeLayer()
    shape.opacity = 0.0

    // create an inital path at the starting position
    shape.path = UIBezierPath(arcCenter: CGPoint.zero,clockwise: true).cgPath
    shape.position = CGPoint.zero


    // create an inital path at the starting position
    shape.path = UIBezierPath(arcCenter: startingPosition,radius: starTradius,startAngle: BubbleConstants.StartingAngle,endAngle: BubbleConstants.EndAngle,clockwise: true).cgPath
    shape.position = startingPosition

    // set the fill color
    shape.fillColor = UIColor.white.cgColor

    layer.addSublayer(shape)

    shape.opacity = Float(opacity)

    dispatchQueue.main.async {
        self.layer.addSublayer(shape)
        CATransaction.begin()
    }

    let radiusAnimation = CABasicAnimation(keyPath: "path")
    radiusAnimation.fromValue = shape.path
    radiusAnimation.tovalue = UIBezierPath(arcCenter: center,radius: endRadius,clockwise: true).cgPath


    dispatchQueue.main.async { [uNowned self] in
        CATransaction.setCompletionBlock { [uNowned self] in

            // remove the shape
            dispatchQueue.main.async {
                shape.removeFromSuperlayer()
                shape.removeAllAnimations()
            }

            dispatchQueue.global(qos: .background).async {
                // spawn a new shape
                self.spawn()
            }
        }
    }


    let movementAnimation = CABasicAnimation(keyPath: "position")
    movementAnimation.fromValue = NSValue(cgPoint: startingPosition)
    movementAnimation.tovalue = NSValue(cgPoint: destination)


    let animationGroup = CustomAnimationGroup()
    animationGroup.animations = [radiusAnimation,movementAnimation]
    animationGroup.fillMode = kCAFillModeForwards
    animationGroup.isRemovedOnCompletion = false
    animationGroup.duration = duration

    shape.add(animationGroup,forKey: "bubble_spawn")

    dispatchQueue.main.async {
        CATransaction.commit()
    }
}

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...