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

时间:2019-03-18 16:51:04

标签: ios swift uiview uikit-dynamics

我将使用UIAttachmentBehavioranchorPoint连接到特定的UIView,然后使用CAShapeLayer画一条线。

enter image description here

我用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文档的教程。

1 个答案:

答案 0 :(得分:0)

您需要将PURCHASE_RECORD ^A%{FIELD01}%{FIELD2}%{FIELD3} BILLING_RECORD ^B%{FIELD01}%{FIELD4}%{FIELD5} PAYMENT_RECORD ^B%{FIELD06}%{FIELD7}%{FIELD8} MYDATA %{PURCHASE_RECORD}|%{BILLING_RECORD}|%{PAYMENT_RECORD} observeValueForKeyPath:ofObject:change:context函数移出updateLine函数。

loadView()函数需要在ViewController级别进行覆盖,因为您已经在observeValueForKeyPath:ofObject:change:context上添加了观察者,在本例中是观察者。

参见下文:

self