gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 02:04:01 28 4
gpt4 key购买 nike

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

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

所以我需要帮助来获取两个大纲 View 的坐标并用它们画一条线(线本身并不难,更多的是获取坐标)。目标是绘制一条线(以编程方式)连接两个大纲 View (f.ex. 从一个边缘到另一个边缘,或从顶部,...) .

我已经尝试过以下:

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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com