CALayer层知识点

CALayer层知识点
  //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);
}
CALayer层知识点



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值