MDRotatingPieChart rotates x / y instead of angle

MDRotatingPieChart Screenshot

I want to have a spinning cake in my iOS app. I found an MDRotatingPieChart control that seems to do most of what I need. But when I put it at the beginning (0,0) and start dragging, it also changes the position of X and Y in the piechart. He should not do that. Can someone help me here?

Here is the code that seems relevant

 pieChart = MDRotatingPieChart(frame: CGRect(x: 30, y: 30, width: view.frame.width + 30 , height: view.frame.width + 30 )) 

and inside MDRotatingPieChart :

 override func continueTracking(_ touch: UITouch, with event: UIEvent?) -> Bool { hasBeenDraged = true let currentPoint = touch.location(in: self) let deltaX = currentPoint.x - pieChartCenter.x - 15 let deltaY = currentPoint.y - pieChartCenter.y - 15 let ang = atan2(deltaY,deltaX); let angleDifference = delta - ang self.transform = self.transform.rotated(by: -angleDifference) let savedTransform = slicesArray[0].labelObj?.transform let savedTransformCenter = labelCenter.transform for slice in slicesArray { if(slice.labelObj != nil) { slice.labelObj?.transform = savedTransform!.rotated(by: angleDifference) } } labelCenter.transform = savedTransformCenter.rotated(by: angleDifference) return true; } 
+5
source share
1 answer

First of all, it looks like you should use MDRotatingPieChart.swift from the MDRotatingPieChart-Example folder , since it is much more modern. However, it contains the error you described, and the source of the error, apparently, is that the original developer does not know the difference between frame and bounds , which correspond to the UIView doc :

The geometry of each view is determined by its frame and bounds properties. The frame property determines the beginning and dimensions of the view in the coordinate system of its supervisor. The bounds property determines the internal dimensions of a view when it sees them, and is used almost exclusively in custom drawing code.

So, to fix the problem, you can find the following line in createSlice

  mask.frame = self.frame 

and replace it with

  mask.frame = self.bounds 

PS There is a line in your code

 pieChart = MDRotatingPieChart(frame: CGRect(x: 30, y: 30, width: view.frame.width + 30 , height: view.frame.width + 30 )) 

what is suspicious. You almost never want the width or height for the child to be larger than the corresponding parent dimension. You probably want

 pieChart = MDRotatingPieChart(frame: CGRect(x: 30, y: 30, width: view.frame.width - 30 , height: view.frame.width - 30 )) 

or even (if you need symmetric fields for both X and Y)

 pieChart = MDRotatingPieChart(frame: CGRect(x: 30, y: 30, width: view.frame.width - 2*30 , height: view.frame.width - 2*30 )) 

instead.

+2
source

Source: https://habr.com/ru/post/1275452/


All Articles