[英]Double value cannot be converted to Int64 because it is either infinite or NaN
[英]Fatal error: Float value cannot be converted to Int because it is either infinite or NaN
我在嘗試將浮點數轉換為 Int 時遇到致命錯誤。
調試:
好的,所以有這條線來跟蹤頁面滾動。
浮動(scrollView.contentOffset.x)/浮動(scrollView.frame.size.width)
這會返回值,例如:
2.031401 2.0241547 2.018116 2.013285 2.009662 2.0072465 2.0048308 2.0036232 2.0024154 2.0012078
我們想把它四舍五入; 我們不希望有一個浮點作為小數點,這可能是草率的中途滾動的結果
這里我使用所以我使用 floorf: floorf(Float(scrollView.contentOffset.x) / Float(scrollView.frame.size.width))
它返回值,如:0.0、1.0、2.0。 完美的。 但是 var currentPage 只需要 Int
現在我們將其轉換為 Int: pageControl.currentPage = Int(floorf(Float(scrollView.contentOffset.x) / Float(scrollView.frame.size.width)))
這會產生以下錯誤:
浮點值無法轉換為 Int,因為它是無窮大或 NaN
完整代碼:
import UIKit
class pageViewController: UIViewController {
private let scrollView = UIScrollView()
private let pageControl: UIPageControl = {
let pageControl = UIPageControl()
pageControl.numberOfPages = 5
pageControl.backgroundColor = .systemBlue
return pageControl
}()
override func viewDidLoad() {
super.viewDidLoad()
scrollView.delegate = self
scrollView.backgroundColor = .red
pageControl.addTarget(self,
action: #selector(pageControlDidChange(_:)),
for: .valueChanged)
view.addSubview(scrollView)
view.addSubview(pageControl)
}
@objc private func pageControlDidChange(_ sender: UIPageControl) {
let current = sender.currentPage
scrollView.setContentOffset(CGPoint(x: CGFloat(current) * view.frame.size.width,
y: 0), animated: true)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
pageControl.frame = CGRect(x: 10,
y: view.frame.size.height-100,
width: view.frame.size.width-20,
height: 70)
scrollView.frame = CGRect(x: 0,
y: 0,
width: view.frame.size.width,
height: view.frame.size.height-100)
if scrollView.subviews.count == 2 {
//2 is because a view by default does not have any subviews. whereas a scroll view does have a subview by default. A scroll view inherently starts with 2 subviews; vertical and horizontal scroll indicators
configureScrollViews()
}
}
private func configureScrollViews() {
scrollView.contentSize = CGSize(width: view.frame.size.width*5, height: view.frame.size.height)
scrollView.isPagingEnabled = true //snaps pages to the middle of the screen
}
}
問題出在下面的函數中
extension pageViewController: UIScrollViewDelegate {
///everytime a scrollView scrolls we want to calculate what page we're on. This sums up this func.
func scrollViewDidScroll(_ scrollView: UIScrollView) {
pageControl.currentPage = Int(floorf(Float(scrollView.contentOffset.x) / Float(scrollView.frame.size.width))) //we want to round it down; we dont want to have a floating point as a decimal point which can be a result of a a sloppy halfway scroll
let colors: [UIColor] = [
.systemRed,
.systemPink,
.systemTeal,
.systemGray,
.systemIndigo
]
for x in 0..<5 {
let page = UIView(frame: CGRect(x: CGFloat(x)*view.frame.size.width, y: 0, width: view.frame.size.width, height: scrollView.frame.size.height))
page.backgroundColor = colors[x]
scrollView.addSubview(page)
}
//print(floorf(Float(scrollView.contentOffset.x) / Float(scrollView.frame.size.width)) )
}
}
您可以在下面的方法中調試並打印以下值:
floorf(Float(scrollView.contentOffset.x) / Float(scrollView.frame.size.width))
並了解數值范圍; 檢查是否正在打印任何NaN
或Infinite
值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.