0

我想将变量引用到特定函数。但是,有一个错误叫做Value of type 'UIView' has no member 'lineTo' Clearly, the whatSelectObjectvariable contains the classes that which members are exists. 所以我使用了 If 语句,“可选绑定”。但结果是一样的。

//: A UIKit based Playground for presenting user interface

import UIKit
import PlaygroundSupport

class ObjectView: UIView {
    var outGoingLine : CAShapeLayer?
    var inComingLine : CAShapeLayer?

    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func lineTo(connectToObj: ObjectView) -> CAShapeLayer {
        let path = UIBezierPath()
        path.move(to: CGPoint(x: self.frame.maxX, y: self.frame.midY))
        path.addLine(to: CGPoint(x: connectToObj.frame.minX, y: connectToObj.frame.midY))

        let line = CAShapeLayer()
        line.path = path.cgPath
        line.lineWidth = 5
        line.fillColor = UIColor.clear.cgColor
        line.strokeColor = UIColor.gray.cgColor
        connectToObj.inComingLine = line
        outGoingLine = line
        return line
    }
}


class MyViewController : UIViewController {
    var whatSelectView: UIView?
    override func loadView() {
        let view = UIView()
        view.backgroundColor = .white

        let object = ObjectView(frame: CGRect(x: 10, y: 100, width: 100, height: 100))
        object.backgroundColor = UIColor.orange
        view.addSubview(object)

        whatSelectView = object

        let object2 = ObjectView(frame: CGRect(x: 200, y: 200, width: 100, height: 100))
        object2.backgroundColor = UIColor.red
        view.addSubview(object2)

        if let s = whatSelectView {
            view.layer.addSublayer(s.lineTo(connectToObj: object2)) // error
        }

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

!此代码是相关简化代码的示例,以帮助受访者理解它。我可以直接引用object变量,但我必须绝对引用whatSelectView

为什么会导致引用变量出错?还是我对可选绑定有误?

4

3 回答 3

1

实际上 UIView 没有任何成员或方法被调用lineTo,你必须使用强制转换(as?在你的情况下)

像这样:

if let s = whatSelectView as? ObjectView {
     view.layer.addSublayer(s.lineTo(connectToObj: object2)) // error
}

除此之外,对的引用whatSelectView应该是弱的,因为视图已经保持对它的强引用,因为它的对象是子视图。

而且您根本不需要 if 条件,您已经使用对象引用了视图。

所以,这是我更好地实施的建议

class MyViewController : UIViewController {
    weak var whatSelectView: ObjectView?

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

        let object = ObjectView(frame: CGRect(x: 10, y: 100, width: 100, height: 100))
        object.backgroundColor = UIColor.orange
        view.addSubview(object)

        whatSelectView = object

        let object2 = ObjectView(frame: CGRect(x: 200, y: 200, width: 100, height: 100))
        object2.backgroundColor = UIColor.red
        view.addSubview(object2)

        let shape = object.lineTo(connectToObj: object2)
        view.layer.addSublayer(shape)

        self.view = view
    }
}
于 2019-03-20T14:04:24.093 回答
0
if let s = whatSelectView as? ObjectView {
   view.layer.addSublayer(s.lineTo(connectToObj: object2))
}

这应该可以解决您的问题。

于 2019-03-20T13:56:35.210 回答
0

改变

var whatSelectView: UIView?

var whatSelectView: ObjectView?

它会编译。正如@Larme所说,当您将其声明为 simpleUIView时,您将无法访问.UIView

于 2019-03-20T13:56:43.727 回答