How to draw a line between two views in Swift 3?

j3141592653589793238

I need help with drawing a simple line between two Outline Views in Swift 3 (Xcode 8). My situation:

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

So I Need help to get the coordinates of both Outline Views and draw a line with them (the line itself is not that difficult, more to get the coordinates). The goal is to draw a line (programmatically) that connects both Outline Views (f.ex. from one edge to the other, or from the top, ...).

I already tried following:

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

But that didn't do anything.

I would appreciate your help!

Thank you

j3141592653589793238

I now solved my problem (more or less) as following:

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

I need to know how to do this on runtime. Do I always have to create a new view in order to draw a path?

A full example:

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

That produces following: Output of program

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to draw a line between two points over an image in swift 3?

How to draw a line in Swift 3

Custom Draw Line Between Two Views Not Working Properly

Draw line between two centers (ov views), Without drawing on the views themself

How to draw vertical separation line between two text line android?

How can I draw a line between two google map native markers? (Ionic 3, cordova)

How to draw a line / link between two points on a D3 map based on latitude / longitude?

Android draw a Horizontal line between views

Draw a 3D line between two patches in matlab

How to draw image dynamically on a canvas line between two cells

How to draw a line between two point in Google map?

How do I draw an Ideal line between two headers?

Matplotlib how to draw vertical line between two Y points

How to draw line between two circle in html canvas element

How do I draw a horizontal line between two circles with CSS?

How to draw a line between two given points in R with plotly?

How to draw a line between two points objective-C

How to re-draw line connected between moveable two UIView

How to draw a line between two points with js and CSS

Draw arrow between on line between two points

How to use PolyLine to draw path between two coordinates in Swift iOS

How can I draw vertical line between Row's views in the Jetpack Compose Canvas?

How to draw a dot line between 3d object in Unity

How to draw line graphs in Swift?

Draw line between two distinct points

Python: Draw line between two coordinates in a matrix

Draw a static line between two divs

R: draw a line between two points in ggplot

Draw a line between two coordinates in OpenLayers 4

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive