简体   繁体   English

layoutIfNeeded导致崩溃

[英]layoutIfNeeded causes a crash

I have a UITextView, that is displaying text When the text is not very large(i'm speaking about 100-200 words), the app is working just fine 我有一个UITextView,它显示文本当文本不是很大时(我说的是100-200个单词),该应用程序运行正常

If it has EXTREMLY large texts(around 10000 words), it keeps crashing 如果它有非常大的文本(大约10000个单词),它将不断崩溃

The code 编码

func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
    check(scrollView)
    let scrollPos = textView.contentOffset.y

    if dragging { return }
    if isAppearanceOpened { return }

    if scrollPos > 0 {
        UIView.animate(withDuration: 0.2, delay: 0, options: .curveEaseInOut, animations: {
            if scrollPos <= self.contentOffset {
                self.topView.alpha = 1
                self.pageView.alpha = 1
                self.topViewHeight.constant = 70
            } else {
                self.topView.alpha = 0
                self.pageView.alpha = 0
                self.topViewHeight.constant = 0
            }
            self.view.layoutIfNeeded()
        }) { (_) in
            if !decelerate { self.recheckEditorPosition() }
        }
    } else {
        UIView.animate(withDuration: 0.2, delay: 0, options: .curveEaseInOut, animations: {
            self.topView.alpha = 1
            self.pageView.alpha = 1
            self.topViewHeight.constant = 70
        }) { (_) in
            if !decelerate { self.recheckEditorPosition() }
        }
    }
}

The error 错误

EXC_BAD_ACCESS (code=2, address=0x16d2a7ef0) EXC_BAD_ACCESS(代码= 2,地址= 0x16d2a7ef0)

Console is clear, so i'm kind of confused If i'm removing self.view.layoutIfNeedee() , the app works just fine, but the animation is absent 控制台很清楚,所以我有点困惑如果我要删除self.view.layoutIfNeedee() ,则应用程序可以正常运行,但是缺少动画

So, i got a result out of my research The crash was caused by the textView contentInset. 因此,我从研究中得到了一个结果。崩溃是由textView contentInset引起的。 When the left and right insets are 0, it's working well on large texts, and my guess is that the calculation of the textView alignements, fonts, and so on are quite time consuming, so the compiler just gave a error. 当左右inset为0时,它在大文本上运行良好,我的猜测是textView对齐方式,字体等的计算非常耗时,因此编译器给出了一个错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM