Reputation: 11175
I am trying to add a circular progress bar to my app in Swift, however I haven't been able to find any Swift specific examples. Therefore, I found the easiest Objective C example and I am trying to translate it into Swift.
This is the example I'm following: Using circular progress bar with masked image?
I am stuck at this line:
CGPoint center = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
CGRectGetMidX does not seem to be available in Swift.
Here is all of my code so far:
func drawRect(rect: CGRect) {
var ctx = UIGraphicsGetCurrentContext()
var progress: CGFloat = 0.7
var innerRadiusRatio: CGFloat = 0.5
var path: CGMutablePathRef = CGPathCreateMutable()
var startAngle: CGFloat = -M_PI_2
var endAngle: CGFloat = -M_PI_2 + MIN(1.0, progress) * M_PI * 2
var outerRadius: CGFloat = CGRectGetWidth(self.bounds) * 0.5 - 1.0
var innerRadius: CGFloat = outerRadius * innerRadiusRatio
}
Where am I going wrong? As well as struggling to translate this line, the other lines have some errors too.
Upvotes: 0
Views: 5182
Reputation: 93276
I don't get an error on that line at all -- `CGRectGetMidX is still available as far as I can tell:
var center = CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect))
The other line that gives me errors is this one -- those constants are of type Double
and need to be converted to CGFloat
, and in Swift the method is min
not MIN
:
var endAngle: CGFloat = CGFloat(-M_PI_2) + min(1.0, progress) * CGFloat(M_PI) * 2
Upvotes: 1