为什么在 .addObserver 中以 NSException 类型的未捕获异常终止

Why terminating with uncaught exception of type NSException in .addObserver

我会用UIAttachmentBehavior连接anchorPoint到特定的UIView,然后用CAShapeLayer画一条线。

我用addObserver每分钟带来UIView的坐标。

但是,出现未知错误。我不认为我熟悉这个功能。

错误: libc++abi.dylib: terminating with uncaught exception of type NSException

代码:

//: A UIKit based Playground for presenting user interface

import UIKit
import PlaygroundSupport

class MyViewController : UIViewController {
    private var animator: UIDynamicAnimator?
    private var gravity: UIGravityBehavior?
    private var attach: UIAttachmentBehavior?
    private var plateView: UIView?

    var lineLayer: CAShapeLayer?

    override func loadView() {
        let view = UIView()
        view.backgroundColor = .white
        self.view = view

        plateView = UIView(frame: CGRect(x: 80, y: 150, width: 60, height: 60))
        plateView?.backgroundColor = UIColor.orange
        self.view.addSubview(plateView!)

        animator = UIDynamicAnimator(referenceView: view)

        gravity = UIGravityBehavior(items: [plateView!])
        animator?.addBehavior(gravity!)

        attach = UIAttachmentBehavior(item: plateView!, offsetFromCenter: UIOffset(horizontal: 0, vertical: -30), attachedToAnchor: CGPoint(x: 200, y: 100))

        attach?.damping = 0.1
        attach?.frequency = 0.6
        animator?.addBehavior(attach!)

        plateView?.addObserver(self, forKeyPath: "center", options: .new, context: nil)

        func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
            updateLine()
        }

        func updateLine() {
            if nil == lineLayer {
                lineLayer = CAShapeLayer()
                lineLayer?.strokeColor = UIColor.purple.cgColor
                lineLayer?.fillColor = UIColor.clear.cgColor
                lineLayer?.lineWidth = 1.5
                lineLayer?.lineJoin = .round
                lineLayer?.strokeEnd = 1.0
                self.view.layer.addSublayer(lineLayer!)
            }

            let platePoint = view.convert(CGPoint(x: plateView!.bounds.midX, y: 0), from: plateView)
            let bezierPath = UIBezierPath()
            bezierPath.move(to: attach!.anchorPoint)
            bezierPath.addLine(to: platePoint)
            lineLayer?.path = bezierPath.cgPath
        }
    }
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()

我查了资料,但是问题太全面了,找不到明确的答案。

我需要了解和理解这个问题的原因。为什么我的代码会出现这个问题?

参考 this 文档中的教程。

您需要将 observeValueForKeyPath:ofObject:change:contextupdateLine 函数移出 loadView() 函数。

observeValueForKeyPath:ofObject:change:context 函数需要在 ViewController 级别覆盖,因为您已经在 self 上添加了观察者,在本例中是视图控制器。

见下文:

import UIKit
import PlaygroundSupport

class MyViewController : UIViewController {
    private var animator: UIDynamicAnimator?
    private var gravity: UIGravityBehavior?
    private var attach: UIAttachmentBehavior?
    private var plateView: UIView?

    var lineLayer: CAShapeLayer?

    override func loadView() {
        let view = UIView()
        view.backgroundColor = .white
        self.view = view

        plateView = UIView(frame: CGRect(x: 80, y: 150, width: 60, height: 60))
        plateView?.backgroundColor = UIColor.orange
        self.view.addSubview(plateView!)

        animator = UIDynamicAnimator(referenceView: view)

        gravity = UIGravityBehavior(items: [plateView!])
        animator?.addBehavior(gravity!)

        attach = UIAttachmentBehavior(item: plateView!, offsetFromCenter: UIOffset(horizontal: 0, vertical: -30), attachedToAnchor: CGPoint(x: 200, y: 100))

        attach?.damping = 0.1
        attach?.frequency = 0.6
        animator?.addBehavior(attach!)

        plateView?.addObserver(self, forKeyPath: "center", options: .new, context: nil)
    }

    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        updateLine()
    }

    func updateLine() {
        if nil == lineLayer {
            lineLayer = CAShapeLayer()
            lineLayer?.strokeColor = UIColor.purple.cgColor
            lineLayer?.fillColor = UIColor.clear.cgColor
            lineLayer?.lineWidth = 1.5
            lineLayer?.lineJoin = .round
            lineLayer?.strokeEnd = 1.0
            self.view.layer.addSublayer(lineLayer!)
        }

        let platePoint = view.convert(CGPoint(x: plateView!.bounds.midX, y: 0), from: plateView)
        let bezierPath = UIBezierPath()
        bezierPath.move(to: attach!.anchorPoint)
        bezierPath.addLine(to: platePoint)
        lineLayer?.path = bezierPath.cgPath
    }

}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()