I'm currently learning how to implement custom controls. I inevitably stumbled upon the CALayer possibility, as using UIImage is not flexible enough when complex animations need to be performed.
I want to use UIView as a "container" for a CALayer so that its width and height are always the same as the UIView (for flexibility purposes).
I subclassed CALayer and overwrote the drawInContext() method.
Here's the result I'm getting on screen:
The drawing looks pixelated and fuzzy. I am using PaintCode to generate the drawing code for me.
Here's the custom CALayer:
class SegmentActive: CALayer {
func frameSetup() -> CGRect {
let frameWidth:CGFloat = superlayer.frame.width
let frameHeight:CGFloat = superlayer.frame.height
let frame = CGRectMake(0, 0, frameWidth, frameHeight)
println("\(superlayer.frame.width) // \(superlayer.frame.height)")
return frame
}
override func drawInContext(ctx: CGContext!) {
UIGraphicsPushContext(ctx)
CalorieCalculatorUI.drawSegControlActiveBase(frameSetup())
UIGraphicsPopContext()
}
}
And here's how I use it:
class ViewController: UIViewController {
@IBOutlet weak var leftSegmentUIView: UIView!
override func viewDidLoad() {
let activeSegment:SegmentActive = SegmentActive()
leftSegmentUIView.layer.addSublayer(activeSegment)
activeSegment.frame = activeSegment.frameSetup()
activeSegment.setNeedsDisplay()
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
What am I doing wrong here? :(