//new a customer layer
CALayer *layer = [CALayer layer];
//set layer's backgroundColor
layer.backgroundColor = [UIColor yellowColor].CGColor;
//set layer's shadeOffset
layer.shadowOffset = CGSizeMake(10, 23);
//set layer's radius
layer.shadowRadius = 4.0;
//set layer's shadeColor
layer.shadowColor = [UIColor purpleColor].CGColor;
//set layer's opacity
layer.shadowOpacity = 0.6;
//set layer's borderwidth
layer.borderWidth = 0.8;
//set layer's borderColor
layer.borderColor = [UIColor colorWithRed:125./255 green:125./255 blue:125./255 alpha:0.7].CGColor;
//set layer's frame
layer.frame = CGRectMake([UIScreen mainScreen].applicationFrame.size.width/2-40/2, self.view.frame.size.height/2-50/2, 40, 50);
//addSubLayer
[self.view.layer addSublayer:layer];
//add imageView content and layer
CALayer *imageLayer = [CALayer layer];
//layer's cornerRadius
imageLayer.cornerRadius = 8.0;
//set imagelayer's frame equal to layer's bounds
imageLayer.frame = layer.bounds;
//set imagelayer‘s corner
imageLayer.masksToBounds = YES;
imageLayer.contents = (id)[UIImage imageNamed:@"Icon@2x.png"].CGImage;
[layer addSublayer:imageLayer];
//customer draw a image base on calyer
step1: creat a sublayer add it to self.calayer
CALayer *customDrawn = [CALayer layer];
customDrawn.delegate = self;
customDrawn.backgroundColor = [UIColor greenColor].CGColor;
customDrawn.frame = CGRectMake(30, 150, 80, 60);
customDrawn.shadowOffset = CGSizeMake(10, 3);
customDrawn.shadowRadius = 5.0;
customDrawn.shadowColor = [UIColor blackColor].CGColor;
customDrawn.shadowOpacity = 0.8;
customDrawn.cornerRadius = 10.0;
customDrawn.borderColor = [UIColor blackColor].CGColor;
customDrawn.borderWidth = 2.0;
customDrawn.masksToBounds = YES;
[self.view.layer addSublayer:customDrawn];
step2:new a function statict inline
static inline double radians (double degrees) { return degrees * M_PI/180; }
void MyDrawColoredPattern (void *info, CGContextRef context) {
CGColorRef dotColor = [UIColor colorWithHue:0 saturation:0 brightness:0.07 alpha:1.0].CGColor;
CGColorRef shadowColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.1].CGColor;
CGContextSetFillColorWit
hColor(context, dotColor);
CGContextSetShadowWithCo
lor(context, CGSizeMake(0, 1), 1, shadowColor);
CGContextAddArc(context, 3, 3, 4, 0, radians(360), 0);
CGContextFillPath(context);
CGContextAddArc(context, 16, 16, 4, 0, radians(360), 0);
CGContextFillPath(context);
}
//mainifold this function
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)context {
CGColorRef bgColor = [UIColor colorWithHue:0.6 saturation:1.0 brightness:1.0 alpha:1.0].CGColor;
//fill color by context
CGContextSetFillColorWit
hColor(context, bgColor);
CGContextFillRect(context, layer.bounds);
static const CGPatternCallbacks callbacks = { 0, &MyDrawColoredPattern, NULL };
CGContextSaveGState(context);
CGColorSpaceRef patternSpace = CGColorSpaceCreatePatter
n(NULL);
CGContextSetFillColorSpa
ce(context, patternSpace);
CGColorSpaceRelease(patternSpace);
CGPatternRef pattern = CGPatternCreate(NULL,
layer.bounds,
CGAffineTransformIdentit
y,
24,
24,
kCGPatternTilingConstant
Spacing,
true,
&callbacks);
CGFloat alpha = 1.0;
CGContextSetFillPattern(context, pattern, &alpha);
CGPatternRelease(pattern);
CGContextFillRect(context, layer.bounds);
CGContextRestoreGState(context);
}