Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Swift 3中的两个视图之间划一条线?_Swift_Xcode_Macos - Fatal编程技术网

如何在Swift 3中的两个视图之间划一条线?

如何在Swift 3中的两个视图之间划一条线?,swift,xcode,macos,Swift,Xcode,Macos,我需要帮助在Swift 3(Xcode 8)中的两个大纲视图之间画一条简单的线。 我的情况: Main ViewController |--- Main View |--- Outline View |--- Outline View 因此,我需要帮助获得两个轮廓视图的坐标,并用它们画一条线(这条线本身并不难,更容易获得坐标)。目标是(以编程方式)绘制一条线,连接两个轮廓视图(例如,从一条边到另一条边,或从顶部,…) 我已经试过了: class Line: NSView{

我需要帮助在Swift 3(Xcode 8)中的两个大纲视图之间画一条简单的线。 我的情况:

Main ViewController
|--- Main View
     |--- Outline View
     |--- Outline View
因此,我需要帮助获得两个轮廓视图的坐标,并用它们画一条线(这条线本身并不难,更容易获得坐标)。目标是(以编程方式)绘制一条线,连接两个轮廓视图(例如,从一条边到另一条边,或从顶部,…)

我已经试过了:

class Line: NSView{
    var origin = CGPoint()
    var destination = CGPoint()

    required init?(coder aDecoder: NSCoder){
        super.init(coder: aDecoder)
    }

    init(fromPoint: CGPoint, toPoint: CGPoint){
        self.origin = fromPoint
        self.destination = toPoint

        super.init(frame: CGRect(origin: fromPoint, size: CGSize(width: destination.x - origin.x, height: destination.y - origin.y)))
    }

    override func draw(_ dirtyRect: NSRect){
        let myPath = NSBezierPath()

        myPath.move(to: CGPoint(x: origin.x, y: origin.y))
        myPath.line(to: CGPoint(x: destination.x - origin.x, y: destination.y - origin.y))
        myPath.stroke()
    }
}

class ViewController: NSViewController{
    override func viewDidLoad(){
        super.viewDidLoad()

       let line = Line(fromPoint: self.view.convert(CGPoint.zero, to: self.view.viewWithTag(1)), toPoint: self.view.convert(CGPoint.zero, to: self.view.viewWithTag(2)))
        view.addSubview(line)
    }
}
但那没用

谢谢你的帮助


谢谢

很多人会认为这太过分了,但我要做的是添加一个子视图,其背景是线条颜色,其高度约束为所需的线条厚度,其前缘和后缘约束为superview的前缘和后缘。这确保了边框将始终随superview的大小进行调整,这就是为什么添加边框作为图层或自定义视图的
绘制(in:)
将边框绘制为路径也不起作用的原因。

我现在解决了我的问题(或多或少),如下所示:

class Line: NSView{
    var fromPoint = CGPoint()
    var toPoint = CGPoint()

    func setPoints(fromPoint: CGPoint, toPoint: CGPoint){
        self.fromPoint = fromPoint
        self.toPoint = toPoint
    }

    override func draw(_ dirtyRect: NSRect) {
        let path = NSBezierPath()

        NSColor.green.setFill()
        path.move(to: fromPoint)
        path.line(to: toPoint)
        path.stroke()
    }
}


class ViewController: NSViewController{
     override function viewDidLoad(){
          super.viewDidLoad()

          let subview3 = Line(frame: self.view.bounds)
          subview3.setPoints(fromPoint: subview1.convert(CGPoint(x: subview1.bounds.maxX, y: subview1.bounds.maxY), to: self.view), toPoint: subview2.convert(CGPoint(x: subview2.bounds.minX, y: subview2.bounds.minY), to: self.view))
          self.view.addSubview(subview3)
     }
}
我需要知道如何在运行时执行此操作。我是否总是必须创建新视图才能绘制路径

一个完整的例子:

//
//  ViewController.swift
//  DrawConnectViews
//
//  Created by T M on 17.06.17.
//  Copyright © 2017 TM. All rights reserved.
//

import Cocoa

class ViewController: NSViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let subview1 = CustomViewWithColor(frame: NSRect(origin: CGPoint(x: 10.0, y: 10.0), size: CGSize(width: 200.0, height: 200.0)))
        let subview2 = CustomViewWithColor(frame: NSRect(origin: CGPoint(x: 360.0, y: 360.0), size: CGSize(width: 200.0, height: 200.0)))

        // create a subview programatically:
        let subview3 = Line(frame: self.view.bounds)
        subview3.setPoints(fromPoint: subview1.convert(CGPoint(x: subview1.bounds.maxX, y: subview1.bounds.maxY), to: self.view), toPoint: subview2.convert(CGPoint(x: subview2.bounds.minX, y: subview2.bounds.minY), to: self.view))
        self.view.addSubview(subview3)

        subview1.setColor(color: NSColor.red)
        subview2.setColor(color: NSColor.blue)
        self.view.addSubview(subview1)
        self.view.addSubview(subview2)
    }

    override var representedObject: Any? {
        didSet {
        // Update the view, if already loaded.
        }
    }



}

class CustomViewWithColor: NSView{
    var color = NSColor()

    func setColor(color: NSColor){
        self.color = color
    }

    override func draw(_ dirtyRect: NSRect) {
        let path = NSBezierPath(rect: self.bounds)
        self.color.setFill()
        path.fill()



    }
}


class Line: NSView{
    var fromPoint = CGPoint()
    var toPoint = CGPoint()

    func setPoints(fromPoint: CGPoint, toPoint: CGPoint){
        self.fromPoint = fromPoint
        self.toPoint = toPoint
    }

    override func draw(_ dirtyRect: NSRect) {
        let path = NSBezierPath()

        NSColor.green.setFill()
        path.move(to: fromPoint)
        path.line(to: toPoint)
        path.stroke()
    }
}
这将产生以下结果:

也许我是在问这个问题,但是为什么不使用分隔符样式
NSBox
()并在Interface Builder中进行布局呢?如果你想做自定义绘图,你也可以将其子类化…@nsgood谢谢,但我想你误解了我的问题,可能是我的错误-我会编辑它。我不想把它们分开,只是想画一条线把它们连接起来(就像从一边到另一边);我想用一条线连接两个视图,而不是将它们分开。没问题:-)你对此有什么想法吗?它在iOS11、iOS12、iOS13和iOS14中不起作用。它只是用不同的颜色绘制两个视图,并用黑色填充视图背景。不再是画绿线了。也许你可以为Swift 5更新你的答案