0

enter image description here

enter image description here

I have UIView class to display lines over the view:

import UIKit

class DrawLines: UIView
{
    override init(frame: CGRect)
    {
        super.init(frame: frame)
    }

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

    override func draw( _ rect: CGRect)
    {
        let context = UIGraphicsGetCurrentContext()
        context!.setLineWidth(2.0)
        context!.setStrokeColor(UIColor.white.cgColor)

        //make and invisible path first then we fill it in
        context!.move(to: CGPoint(x: 0, y: 0))
        context!.addLine(to: CGPoint(x: self.bounds.width, y:self.bounds.height))
        context!.strokePath()
    }
}

And main class to call it...

import UIKit

class GraphViewController: UIViewController
{
    @IBOutlet weak var graphView: UIView!
    override func viewDidLoad()
    {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        let draw = DrawLines(frame: self.graphView.bounds)
        view.addSubview(draw)
    }

    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        if UIDevice.current.orientation.isLandscape
        {
            print("landscape")
        }
        else
        {
            print("portrait")
        }
    }
}

However, there is an issue when i rotate the screen. As i understood the problem is - it always use the height and width of the screen, so that i should check landscape orientation and put:

let yLandscaped = self.bounds.width
let xLandscaped = self.bounds.height

But i don't know, how to clear all lines inside of the view?

J A S K I E R
  • 1,976
  • 3
  • 24
  • 42

1 Answers1

0

When i tried to rotate - it takes previous bounds of view as i understood. So that i reversed X and Y origins of view. But when you load it at first it should be just view.bounds. However i tried to cut the height of image -10 and below it, there should an empty space, but there is a part of the same image before it was turned! To fix it, just need to put

while let subview = self.view.subviews.last
        {   subview.removeFromSuperview()   }

before each rotation. Works great!

override func viewDidLoad()
{
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    let draw = DrawLines(frame: self.view.bounds)
    view.addSubview(draw)
}

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    if UIDevice.current.orientation.isLandscape
    {
        print("landscape")
        while let subview = self.view.subviews.last
        {   subview.removeFromSuperview()   }
        let draw = DrawLines(frame: CGRect(x: self.view.bounds.origin.y, y: self.view.bounds.origin.x, width: self.view.bounds.height, height: self.view.bounds.width))
        view.addSubview(draw)
    }

    else
    {
        print("portrait")
        while let subview = self.view.subviews.last
        {   subview.removeFromSuperview()   }
        let draw = DrawLines(frame: CGRect(x: self.view.bounds.origin.y, y: self.view.bounds.origin.x, width: self.view.bounds.height, height: self.view.bounds.width))
        view.addSubview(draw)
    }
}
J A S K I E R
  • 1,976
  • 3
  • 24
  • 42