swift - 如何在 Swift 3 中的两个 View 之间画一条线?

标签 swift xcode macos

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

Main ViewController
|--- Main View
     |--- Outline View
     |--- Outline View

所以我需要帮助来获取两个大纲 View 的坐标并用它们画一条线(线本身并不那么困难,更多的是获取坐标)。目标是(以编程方式)绘制一条线连接两个大纲 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)
    }
}

但这并没有起到任何作用。

非常感谢您的帮助!

谢谢

最佳答案

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

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)
     }
}

我需要知道如何在运行时执行此操作。我是否总是需要创建一个新 View 才能绘制路径?

完整示例:

//
//  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()
    }
}

这会产生以下结果: Output of program

关于swift - 如何在 Swift 3 中的两个 View 之间画一条线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44593598/

相关文章:

macos - 无法在 Jenkins 中运行 "pod install"

ios - C/XCode : sh: ./child : No such file or directory. 命令在终端中运行,但不在 XCode 中运行

swift - 使用未声明的类型 'GraphQLMappable' Apollo-iOS

c++ - 将 C++ 代码添加到 iOS 项目

xcode - SDL_mixer.h发生了什么?

ios - navigationBar backButton 标题存在,但在实例化的 viewController 上缺少箭头

ios - 如何在 iOS 上启用文件和文件夹权限

ios - 表格 View 单元格的外壳。 swift

ios - 如何在不可变值上使用变异成员?

objective-c - 如何自定义 NSSlider