显示隐藏视图非常慢

时间:2018-02-14 10:19:47

标签: ios swift uiview swift4

根据这些SO线程,我知道UI组件应该通过主UI线程更新,但是我仍然无法解决问题。我隐藏的视图在超过20秒后仍然显示。真的很慢!

iOS SWIFT Showing hidden view really slow

Swift/iOS : UIPicker display is not refreshed until tapped after changing the datasource programmatically

详细说明,以下是我正在做的事情:

的ViewController

override func viewDidLoad() {

  let campusTapGesture = UITapGestureRecognizer(target: self, action: #selector(handleCampusOpenTap))

        campusTapGesture.numberOfTapsRequired = 1 // Default
        campusSelect.isUserInteractionEnabled = true
        campusSelect.addGestureRecognizer(campusTapGesture)

  let networkController = NetworkController()
        networkController.fetchCampusList(empId: empId, terminal: "IOS-011", handler: self)
}

虽然campusSelectUIView,但点击它会显示隐藏的视图。

我的NetworkController有一个协议方法func onCampusResponseSuccess(response: CampusListResponse)ViewController符合这种方式:

func onCampusResponseSuccess(response: CampusListResponse) {

    let listOfCampus = convertToCampusListModel(apiResponse: response)
    campusPickerData = CampusPickerData(list: listOfCampus)

    // Below code block is performed on main thread because this method runs on background thread and only being invoked when list is received from API
    DispatchQueue.main.async {
        self.campusPick.dataSource = self.campusPickerData
        self.campusPick.delegate = self.campusPickerData
        self.selectedCampus.text = listOfCampus[0].description
    }

    campusResponseException = false
    campusResponseFail = false

    hideAlert(alert: progressAlert)

}

当我点击campusSelect UIView时,似乎应用程序处于睡眠模式并且显示隐藏视图需要超过20秒,同样隐藏它。

这是我的手势点击:

@objc func handleCampusOpenTap(sender: UITapGestureRecognizer) {
    DispatchQueue.main.async {
      self.campusContainer.isHidden = false
    }
}

这是用于隐藏UIView

的按钮的动作
@IBAction func doneCampusSelectClick(_ sender : UIButton){
    let campus = campusPickerData.campusList[campusPick.selectedRow(inComponent: 0)].description

    selectedCampus.text = campus
    DispatchQueue.main.async {
      self.campusContainer.isHidden = true
    }
}

我做错了什么!我的UIPickerView显示在隐藏视图中。是因为我有UIPickerView dataSourcedelegate的自定义类,还是与Threading有关?

为了更清楚,这是我的NetworkController的方法:

let task = session.dataTask(with: request as URLRequest, completionHandler: {data, response, error in
        guard error == nil else{
            handler.onCampusResponseFail()
            return
        }

        guard let data = data else {
            DispatchQueue.main.async {
                handler.onCampusResponseException()
            }
            return
        }

        do {
            let jsonDecoder = JSONDecoder()

            let campusListResponse = try jsonDecoder.decode(CampusListResponse.self, from: data)

            DispatchQueue.main.async {
                handler.onCampusResponseSuccess(response: campusListResponse)
            }

        } catch let error {
            DispatchQueue.main.async {
                handler.onCampusResponseException()
            }
            print(error.localizedDescription)
        }
    })
    task.resume()

很抱歉很长的帖子!

1 个答案:

答案 0 :(得分:0)

请隐藏并在主线程中显示视图,即

//隐藏

@objc func handleCampusOpenTap(sender: UITapGestureRecognizer) {
   DispatchQueue.main.async {
     self.campusContainer.isHidden = false
   }
}

同样用于展示,

@IBAction func doneCampusSelectClick(_ sender : UIButton){
    let campus = campusPickerData.campusList[campusPick.selectedRow(inComponent: 0)].description

    selectedCampus.text = campus
    DispatchQueue.main.async {
      campusContainer.isHidden = true
    }
}

希望它会有所帮助,因为其余的代码很好。