I drawn an image previously (on main thread, synchronously), it works well. Below is what I drawn:
But now, I'd like to utilize GCD to redraw it for improving performance. But I got an image like below after I turn the code to use GCD:
Why the gray circle goes sideway?
This is the latest code (asynchronously):
let queue3 = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
print("frame: \(self.frame)")
self.attachSunMoon()
self.attachClock(rect)
dispatch_async(queue3, {
self.buildCircle(circleRadius: self.radius)
})
Before, the code is (synchronously, this works well, but not what I want, I prefer to utilize GCD):
print("frame: \(self.frame)")
self.attachSunMoon()
self.attachClock(rect)
self.buildCircle(circleRadius: self.radius)
At last, the buildCircle
function:
/// Draw the big circle
private func buildCircle(circleRadius radius: CGFloat) {
UIGraphicsPushContext(self.ccontext!)
defer { UIGraphicsPopContext() }
print("frame: \(self.frame)")
// Drawing code
let center = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2)
self.centerPoint = center
//let radius = max(rect.width, rect.height) - 40
let startAngle: CGFloat = 0
let endAngle: CGFloat = 2*π
let path = UIBezierPath(arcCenter: center, radius: radius, startAngle: startAngle, endAngle: endAngle, clockwise: true)
path.lineWidth = self.arcWidth
clockBorderColor.setStroke()
path.stroke()
}
Appreciate your help! :)