CALayer层知识点

来源:互联网 发布:linux中如何测试网速 编辑:程序博客网 时间:2024/06/01 11:19
CALayer层知识点
 //new a customer layer
    CALayer*layer = [CALayer layer];
    //setlayer's backgroundColor
   layer.backgroundColor = [UIColor yellowColor].CGColor;
    //setlayer's shadeOffset
   layer.shadowOffset = CGSizeMake(10, 23);
    //setlayer's radius
   layer.shadowRadius = 4.0;
    //setlayer's shadeColor
   layer.shadowColor = [UIColor purpleColor].CGColor;
    //setlayer's opacity
   layer.shadowOpacity = 0.6;
    //setlayer's borderwidth
   layer.borderWidth = 0.8;
    //setlayer's borderColor
   layer.borderColor = [UIColor colorWithRed:125./255 green:125./255blue:125./255 alpha:0.7].CGColor;
    //setlayer's frame
    layer.frame= CGRectMake([UIScreenmainScreen].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'scornerRadius
   imageLayer.cornerRadius = 8.0;
    //setimagelayer's frame equal to layer's bounds
   imageLayer.frame = layer.bounds;
    //setimagelayer‘s corner
   imageLayer.masksToBounds = YES;
   imageLayer.contents = (id)[UIImageimageNamed:@"Icon@2x.png"].CGImage;
    [layeraddSublayer: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){
   
    CGColorRefdotColor = [UIColor colorWithHue:0 saturation:0 brightness:0.07alpha:1.0].CGColor;
    CGColorRefshadowColor = [UIColor colorWithRed:1 green:1 blue:1alpha:0.1].CGColor;
   
   CGContextSetFillColorWithColor(context, dotColor);
   CGContextSetShadowWithColor(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{
   
    CGColorRefbgColor = [UIColor colorWithHue:0.6 saturation:1.0 brightness:1.0alpha:1.0].CGColor;
    //fill colorby context
   CGContextSetFillColorWithColor(context, bgColor);
   CGContextFillRect(context, layer.bounds);
   
    static constCGPatternCallbacks callbacks = { 0,&MyDrawColoredPattern, NULL };
   
   CGContextSaveGState(context);
   CGColorSpaceRef patternSpace =CGColorSpaceCreatePattern(NULL);
   CGContextSetFillColorSpace(context, patternSpace);
   CGColorSpaceRelease(patternSpace);
   
    CGPatternRefpattern = CGPatternCreate(NULL,
                                          layer.bounds,
                                          CGAffineTransformIdentity,
                                          24,
                                          24,
                                          kCGPatternTilingConstantSpacing,
                                          true,
                                          &callbacks);
    CGFloatalpha = 1.0;
   CGContextSetFillPattern(context, pattern,&alpha);
   CGPatternRelease(pattern);
   CGContextFillRect(context, layer.bounds);
   CGContextRestoreGState(context);
}
CALayer层知识点



0 0
原创粉丝点击