首页 > 解决方案 > 按下 navigationItem 按钮时,活动指示器没有动画

问题描述

当按下导航项的按钮时,我尝试触发活动指示器的动画。但我发现活动指示器没有旋转。我试图把scanerIndicator.startAnimating()主线程,但没有帮助。

代码收集路由器打开的端口,我想在按下navigationItem按钮时开始旋转,在返回openPorts时停止旋转。感谢任何关于哪里出错的线索/提示?

    override func viewDidLoad() {
        super.viewDidLoad()
        ...
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Start", style: .plain, target: self, action: #selector(startScan))
        ...
    }

    @objc func startScan() {
        scanerIndicator.startAnimating()
        
        if let address = serverAddress.text, !address.isEmpty {
            if let start = Int(startPort.text!) {
                if let stop = Int(stopPort.text!) {
                    if start < stop {
                        openPorts = netUtility.scanPorts(address: address, start: start, stop: stop)
                        print("Open Open: \(openPorts)")
                        if !openPorts.isEmpty {
                            scanerIndicator.stopAnimating()
                            table.reloadData()
                        } else {
                            showErrorMessage(errorTitle: "Not at all", errorMessage: "No open ports were found")
                        }
                    } else {
                        showErrorMessage(errorTitle: "Range error", errorMessage: "Start port should be smaller than stop port")
                    }
                } else {
                    showErrorMessage(errorTitle: "Empty fields", errorMessage: "Please fill all the necessary data")
                }
            } else {
                showErrorMessage(errorTitle: "Empty fields", errorMessage: "Please fill all the necessary data")
            }
        } else {
            showErrorMessage(errorTitle: "Empty fields", errorMessage: "Please fill all the necessary data")
        }
    }

收集端口的代码:

   // MARK: - Port Scaner
    // Get number of threads for scan ports
    func getSegmentsQueues(min: Int, max: Int, maxPerSegment: Int) -> [[Int]] {
        
        var start: Int = min
        var portSegments = [[Int]]()
        
        while start <= max {
            var _portSegment = [Int]()
            
            for _ in 1...maxPerSegment {
                
                if start <= max {
                    _portSegment.append(start)
                }
                
                start += 1
            }
            
            portSegments.append(_portSegment)
        }
        
        return portSegments
    }


    // Crate queques for scan ports by segments
    func QueueDispatchPort(address: String, minPort: Int, maxPort: Int, segmentsQueues: (Int, Int, Int) -> [[Int]]) -> [Int] {
        var openPorts : [Int] = []
        let segmentPorts = segmentsQueues(minPort, maxPort, 1);
        
        let group = DispatchGroup()
        
        for segment in segmentPorts {
            group.enter()
            DispatchQueue.global().async {
                
                for port in segment {
                    let client = TCPClient(address: address, port: Int32(port))
                    switch client.connect(timeout: 2) {
                        case .success:
                            openPorts.append(port)
                        
                        case .failure(_):
                            print("port \(port) closed")
                    }
                    
                    client.close()
                }
                group.leave()
            }
        }
        
        group.wait()

        return openPorts
    }
    
    // Scans ports from an address and a range given by the user
    func scanPorts(address : String, start : Int, stop : Int) -> [Int] {
        let openPorts = QueueDispatchPort(
            address: address, minPort: start, maxPort: stop, segmentsQueues:
            getSegmentsQueues(min:max:maxPerSegment:))
        
        return openPorts
    }

代码更新,我将代码块(扫描端口)放在主线程上,stopAnimating()这次删除。在长时间运行代码返回(DispatchQueue.main 中的内容)后,activityIndi​​cator 会被动画化。还是不行...

@objc func startScan() {
        scanerIndicator.startAnimating()
        
        DispatchQueue.main.async { [self] in
            if let address = serverAddress.text, !address.isEmpty {
                if let start = Int(startPort.text!) {
                    if let stop = Int(stopPort.text!) {
                        if start < stop {
                            openPorts = netUtility.scanPorts(address: address, start: start, stop: stop)
                            print("Open Open: \(openPorts)")
                            if !openPorts.isEmpty {
                                table.reloadData()
                            } else {
                                showErrorMessage(errorTitle: "Not at all", errorMessage: "No open ports were found")
                            }
                        } else {
                            showErrorMessage(errorTitle: "Range error", errorMessage: "Start port should be smaller than stop port")
                        }
                    } else {
                        showErrorMessage(errorTitle: "Empty fields", errorMessage: "Please fill all the necessary data")
                    }
                } else {
                    showErrorMessage(errorTitle: "Empty fields", errorMessage: "Please fill all the necessary data")
                }
            } else {
                showErrorMessage(errorTitle: "Empty fields", errorMessage: "Please fill all the necessary data")
            }
        }
    }

标签: swiftuiactivityindicatorview

解决方案


有点难以理解您的代码在做什么,但我的猜测是,即使您使用队列进行端口扫描,因为您使用的是 DispatchGroup,代码会阻塞,直到所有端口扫描完成。

如果您有执行以下操作的同步代码:

  1. 开始动画活动指示器
  2. 执行长时间运行的任务(在主线程上)
  3. 停止动画活动指示器

然后你永远看不到动画。问题是动画在您的代码返回并且您的应用程序访问其事件循环之前不会开始。

相反,您需要像这样编写代码:

scanerIndicator.startAnimating()
DispatchQueue.main.async {
   //Do long-running task
   scanerIndicator.stopAnimating()
}

这是有效的,因为在调用之后startAnimating(),您将异步调用添加到主调度队列(在主线程上),然后返回。您的应用程序的函数调用堆栈全部返回,您的应用程序访问事件循环,活动指示器开始旋转。然后系统会选择您添加到主队列的异步任务并开始运行该任务。最后,当您的长时间运行的任务完成时,您关闭活动指示器(在调用内部async()调用的代码中。


推荐阅读