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?