Wie CGGraphics durch andere Ausrichtung neu gezeichnet werden?
I UIView Klasse Linien über die Ansicht anzuzeigen:
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()
}
}
Und Hauptklasse zu nennen ...
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")
}
}
}
Allerdings gibt es ein Problem, wenn ich den Bildschirm drehen. Wie ich das Problem verstanden - es ist immer die Höhe und Breite des Bildschirms verwenden, so dass ich im Querformat überprüfen sollte, und setze:
let yLandscaped = self.bounds.width
let xLandscaped = self.bounds.height
Aber ich weiß nicht, wie man alle Linien innerhalb der löschen Aussicht?
hoffe, dies wird Ihnen helfen: https://stackoverflow.com/questions/2526162/iphone-sdk-how-to-trigger-drawrect-on-uiview-subclass-after-orientation-change/9023348 –
Dies löst nur die Mehrfachausgabe, aber das Problem bleibt. – Oleksandr