- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 dataViewController,它需要来自 API 的数据才能填充图表和一些新闻。新闻 API 调用非常快速且简单,但图形调用完全减慢了页面滚动速度,因为它是在 viewDidLoad() 中调用的。我编辑了图形API来下载数据,然后将其存储到缓存中,而不是在viewDidLoad上,它检查缓存中是否有任何数据,然后如果有的话就使用它,但滚动页面时仍然非常慢。我怎样才能解决这个问题?以下是我的一些 DataViewController 代码,用于处理与图表有关的所有内容:
extension DataViewController {
func GetOnlyDateMonthYearFromFullDate(currentDateFormate:NSString , conVertFormate:NSString , convertDate:NSString ) -> NSString
{
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = currentDateFormate as String
let formatter = DateFormatter()
formatter.dateFormat = "yyyy'-'MM'-'dd'-'HH':'mm':'ssZZZ" as String
let finalDate = formatter.date(from: convertDate as String)
formatter.dateFormat = conVertFormate as String
let dateString = formatter.string(from: finalDate!)
return dateString as NSString
}
//Charts
@objc func handleLongPress(longPressGesture:UILongPressGestureRecognizer) {
let p = longPressGesture.location(in: self.chartView)
if longPressGesture.state == UIGestureRecognizer.State.began && isLineChartExpanded == false {
mainLabelContainer.fadeOut()
chartViewExpandedConstraints()
isLineChartExpanded = true
UIView.animate(withDuration: 0.5) {
self.view.layoutIfNeeded()
}
} else if longPressGesture.state == UIGestureRecognizer.State.began && isLineChartExpanded == true {
mainLabelContainer.fadeIn()
chartViewLandscapeConstraints()
isLineChartExpanded = false
UIView.animate(withDuration: 0.5) {
self.view.layoutIfNeeded()
}
}
}
func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {
var xInt = Int()
//The Currency Unit taken from the exchange section of the API.
xInt = Int(entry.x)
let currencyUnit = CGExchange.shared.exchangeData[0].rates[defaultCurrency]!.unit
chartPriceLabel.textColor = UIColor.white.withAlphaComponent(0.5)
chartDateLabel.textColor = UIColor.white.withAlphaComponent(0.5)
chartPriceLabel.isHidden = false
chartDateLabel.isHidden = false
chartPriceLabel.text = "\(currencyUnit)\(round(1000*entry.y)/1000)"
let date = self.GetOnlyDateMonthYearFromFullDate(currentDateFormate: "yyyy-MM-dd'T'HH:mm:ss.SSSZ", conVertFormate: "MMM d, h:mm a", convertDate: self.days[xInt] as NSString)
chartDateLabel.text = "\(date as String)"
}
//Graph Buttons and states
enum GraphStat {
case day, fortnight, month
}
@objc func todayButtonAction(sender: UIButton!) {
self.prices = []
self.days = []
CGCharts.shared.graphSetup = .day
CGCharts.shared.getData(coin: self.dataObject, defaultCurrency: self.defaultCurrency, arr: true, completion: { (success) in
self.prepareGraph(arr: true, completion: { (success) in
DispatchQueue.main.async {
self.chartView.animate(xAxisDuration: 4.0)
}
})
})
}
@objc func fortnightButtonAction(sender: UIButton!) {
self.prices = []
self.days = []
CGCharts.shared.graphSetup = .week
CGCharts.shared.getData(coin: self.dataObject, defaultCurrency: self.defaultCurrency, arr: true, completion: { (success) in
self.prepareGraph(arr: true, completion: { (success) in
DispatchQueue.main.async {
self.chartView.animate(xAxisDuration: 4.0)
}
})
})
}
@objc func monthButtonAction(sender: UIButton!) {
self.prices = []
self.days = []
CGCharts.shared.graphSetup = .month
CGCharts.shared.getData(coin: self.dataObject, defaultCurrency: self.defaultCurrency, arr: true, completion: { (success) in
self.prepareGraph(arr: true, completion: { (success) in
DispatchQueue.main.async {
self.chartView.animate(xAxisDuration: 4.0)
}
})
})
}
func lineChartUpdate(dataPoints: [String], values: [Double]) {
if CGCharts.shared.graphSetup == .day {
graphSetup = .day
} else if CGCharts.shared.graphSetup == .week {
graphSetup = .fortnight
} else if CGCharts.shared.graphSetup == .month {
graphSetup = .month
}
//Graph State buttons switch status for highlighting buttons.
switch graphSetup {
case .day:
todayButton.alpha = 0.5
fortnightButton.alpha = 1.0
monthButton.alpha = 1.0
case .fortnight:
fortnightButton.alpha = 0.5
todayButton.alpha = 1.0
monthButton.alpha = 1.0
case .month:
monthButton.alpha = 0.5
todayButton.alpha = 1.0
fortnightButton.alpha = 1.0
}
//Graph data management
var lineChartEntry = [ChartDataEntry]()
for i in 0..<prices.count {
//Graph marker from extension
if prices != [] {
let value = ChartDataEntry(x: Double(i), y: values[i])
lineChartEntry.append(value)
let line1 = LineChartDataSet(values: lineChartEntry, label: "Price")
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .medium
dateFormatter.timeStyle = .none
dateFormatter.locale = Locale(identifier: "en_US")
let dateObjects = self.days.compactMap { dateFormatter.date(from: $0) }
let dateStrings = dateObjects.compactMap { dateFormatter.string(from: $0) }
self.chartView.xAxis.valueFormatter = DefaultAxisValueFormatter(block: {(index, _) in
return dateStrings[Int(index)]
})
line1.setColor(.white)
line1.drawVerticalHighlightIndicatorEnabled = true
line1.drawHorizontalHighlightIndicatorEnabled = true
line1.mode = .cubicBezier
line1.lineWidth = 2.0
line1.drawValuesEnabled = true
line1.valueTextColor = UIColor.white
line1.drawCirclesEnabled = false
chartView.xAxis.valueFormatter = IndexAxisValueFormatter(values:dateStrings)
chartView.xAxis.granularity = 1
chartView.leftAxis.drawGridLinesEnabled = false
chartView.xAxis.drawGridLinesEnabled = false
//Expanded
chartView.rightAxis.enabled = false
chartView.leftAxis.enabled = false
chartView.xAxis.enabled = false
chartView.rightAxis.drawGridLinesEnabled = false
chartView.legend.enabled = false
chartView.dragEnabled = false
chartView.pinchZoomEnabled = false
chartView.drawMarkers = false
chartView.doubleTapToZoomEnabled = false
chartView.isUserInteractionEnabled = true
//Graph Data.
let data = LineChartData()
data.addDataSet(line1)
chartView.data = data
}
}
}
//Dismiss Keyboard when Tap
override func touchesBegan(_ touches: Set<UITouch>,
with event: UIEvent?) {
self.view.endEditing(true)
}
//GraphData
func prepareGraph(arr: Bool, completion: @escaping (Bool) -> ()) {
if Storage.fileExists("\(dataObject)GraphData", in: Storage.Directory.caches) {
print("Exists")
self.priceData = Storage.retrieve("\(dataObject)GraphData", from: Storage.Directory.caches, as: [Price].self)
self.days = self.priceData.map({ $0.date.description })
self.prices = self.priceData.map({ $0.price })
DispatchQueue.main.async {
// self.chartView.animate(xAxisDuration: 4.0)
self.lineChartUpdate(dataPoints: self.days, values: self.prices)
}
} else {
self.prices = []
self.days = []
print("didn'tExist")
CGCharts.shared.graphSetup = .day
print("ChartsCleared")
CGCharts.shared.getData(coin: self.dataObject, defaultCurrency: self.defaultCurrency, arr: true) { (success) in
self.updateGraph()
DispatchQueue.main.async {
self.lineChartUpdate(dataPoints: self.days, values: self.prices)
}
}
}
}
//GraphData In Storage
func updateGraph() {
self.priceData = Storage.retrieve("\(dataObject)GraphData", from: Storage.Directory.caches, as: [Price].self)
self.days = self.priceData.map({ $0.date.description })
self.prices = self.priceData.map({ $0.price })
DispatchQueue.main.async {
// self.chartView.animate(xAxisDuration: 4.0)
self.lineChartUpdate(dataPoints: self.days, values: self.prices)
}
}
}
以下是来自 API 管理器文件的实际 API 调用:
import Foundation
struct Root: Codable {
let prices: [Price]
}
struct Price: Codable {
let date: Date
let price: Double
}
class CGCharts {
var priceData = [Price]()
static let shared = CGCharts()
var currency = ""
var days = ""
enum GraphStatus {
case day, week, month
}
var graphSetup = GraphStatus.day
func getAllCharts(arr: Bool, completion: @escaping (Bool) -> ()) {
}
func getData(coin: String, defaultCurrency: String, arr: Bool, completion: @escaping (Bool) -> ()) {
switch graphSetup {
case .day:
days = "1"
case .week:
days = "14"
case .month:
days = "30"
}
let urlJSON = "https://api.coingecko.com/api/v3/coins/\(coin)/market_chart?vs_currency=\(defaultCurrency)&days=\(days)"
guard let url = URL(string: urlJSON) else { return }
URLSession.shared.dataTask(with: url) { (data, response, err) in
guard let data = data else { return }
do {
let prices = try JSONDecoder().decode(Root.self, from: data).prices
print(prices.first!.date.description(with:.current)) // "Saturday, September 1, 2018 at 6:25:38 PM Brasilia Standard Time\n"
print(prices[0].price)
self.priceData = prices
Storage.store(self.priceData, to: Storage.Directory.caches, as: "\(coin)GraphData")
completion(arr)
} catch {
print(error)
}
}.resume()
}
}
extension Price {
public init(from decoder: Decoder) throws {
var unkeyedContainer = try decoder.unkeyedContainer()
let date = try unkeyedContainer.decode(UInt64.self).date
let price = try unkeyedContainer.decode(Double.self)
self.init(date: date, price: price)
}
public func encode(to encoder: Encoder) throws {
var container = encoder.unkeyedContainer()
try container.encode(date.unixEpochTime)
try container.encode(price)
}
}
extension UInt64 {
var date: Date {
return Date(timeIntervalSince1970: TimeInterval(self)/1000)
}
}
extension Date {
var unixEpochTime: UInt64 {
return UInt64(timeIntervalSince1970*1000)
}
}
这两个文件之一的某些内容太慢,使得页面滚动非常烦人。它只有大约 2 秒,但它将分页动画卡住了 2 秒,我需要找到一种方法使滚动平滑且响应灵敏,即使这意味着在加载之前的几秒钟内不显示任何图表数据。我怎样才能做到这一点,到底是什么让一切变得如此缓慢?我也怀疑这可能与以下内容有关:
self.priceData = Storage.retrieve("\(dataObject)GraphData", from: Storage.Directory.caches, as: [Price].self)
self.days = self.priceData.map({ $0.date.description })
self.prices = self.priceData.map({ $0.price })
因为这是循环遍历一个包含数据的大结构,然后将它们添加到 2 个数组中,也许这会在发生时阻止一切?谢谢。
最佳答案
DispatchQueue.global(qos: .userInitiated).async { [weak self] in
guard let self = self else {
return
}
self.priceData = Storage.retrieve("\(dataObject)GraphData", from:Storage.Directory.caches, as: [Price].self)
self.days = self.priceData.map({ $0.date.description })
self.prices = self.priceData.map({ $0.price })
DispatchQueue.main.async { [weak self] in
// update your UI here based on the data
}
}
关于ios - 如何在Data ViewController的viewDidLoad中加载数据,而不减慢uipageViewController滚动速度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52920145/
我正在开发一个需要能够平均三个数字的 Facebook 应用程序。但是,它总是返回 0 作为答案。这是我的代码: $y = 100; $n = 250; $m = 300; $number = ($y
我只是无法弄清楚这一点,也找不到任何对我来说有意义的类似问题。我的问题:我从数据库中提取记录,并在我的网页上以每个面板 12 条的倍数显示它们。因此,我需要知道有多少个面板可以使用 JavaScrip
我是一名优秀的程序员,十分优秀!