From afc1be65f88ca787d2b80add1711d8f275b123d4 Mon Sep 17 00:00:00 2001 From: Leo Mehlig Date: Thu, 10 Mar 2016 10:50:28 +0100 Subject: [PATCH] Keep position on rotation. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I’ve added a flag to BarLineChartViewBase indicating whether the chart should stay at its current position or not The flag defaults to false to not change any existing behaviour. --- .../Classes/Charts/BarLineChartViewBase.swift | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/Charts/Classes/Charts/BarLineChartViewBase.swift b/Charts/Classes/Charts/BarLineChartViewBase.swift index 5a4b5c34e3..90403f9759 100644 --- a/Charts/Classes/Charts/BarLineChartViewBase.swift +++ b/Charts/Classes/Charts/BarLineChartViewBase.swift @@ -52,6 +52,9 @@ public class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChar /// Sets the minimum offset (padding) around the chart, defaults to 10 public var minOffset = CGFloat(10.0) + /// flag indicating if the chart should stay at the same position after a rotation or not. Default is false. + public var keepPositionOnRotation: Bool = false + /// the object representing the left y-axis internal var _leftAxis: ChartYAxis! @@ -135,6 +138,27 @@ public class BarLineChartViewBase: ChartViewBase, BarLineScatterCandleBubbleChar #endif } + public override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer) + { + //Saving current position of chart. + var oldPoint: CGPoint? + if (keepPositionOnRotation && (keyPath == "frame" || keyPath == "bounds")) + { + oldPoint = viewPortHandler.contentRect.origin + getTransformer(.Left).pixelToValue(&oldPoint!) + } + + //Superclass transforms chart. + super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context) + + //Restoring old position of chart + if var newPoint = oldPoint where keepPositionOnRotation + { + getTransformer(.Left).pointValueToPixel(&newPoint) + viewPortHandler.centerViewPort(pt: newPoint, chart: self) + } + } + public override func drawRect(rect: CGRect) { super.drawRect(rect)