IOS Core Animation learning Summary (2) -- Implement custom layers, iosanimation
1. Create a layer that inherits from CALayer and implements the drawInContext method in the subclass.
@ Interface CTLayer: CALayer @ end @ implementation CTLayer-(void) drawInContext :( CGContextRef) ctx {
// Draw a circle CGContextSetRGBFillColor (ctx, 0, 0, 1, 1); CGContextAddEllipseInRect (ctx, CGRectMake (100,100,100,100); CGContextFillPath (ctx);} @ end
When viewcontroller loads a view,
CTLayer * layer = [CTLayer layer]; layer. bounds = CGRectMake (0, 0,300,300); layer. anchorPoint = CGPointMake (0, 0); [layer setNeedsDisplay]; // display layer [self. view. layer addSublayer: layer];
Ii. Create Using proxy
CTLayer * layer = [CTLayer layer]; layer. bounds = CGRectMake (0, 0,300,300); layer. anchorPoint = CGPointMake (0, 0); layer. delegate = self; // specifies the proxy, which can be any type of [layer setNeedsDisplay]; // display layer [self. view. layer addSublayer: layer];
Implement proxy Methods
# Pragma mark proxy method-(void) drawLayer :( CALayer *) layer inContext :( CGContextRef) ctx {CGContextSetRGBFillColor (ctx, 0, 0, 1, 1); CGContextAddEllipseInRect (ctx, CGRectMake (100,100,100,100); CGContextFillPath (ctx );}