本文主要是介绍CALayer的简单使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
原文地址:http://www.raywenderlich.com/2502/calayers-tutorial-for-ios-introduction-to-calayers-tutorial
如果你已经在iPhone上做过开发,你可能对UIView和它的子类-Button,text,slider等,非常熟悉。
什么是层?
开始
- // Import QuartzCore.h at the top of the file
- #import <QuartzCore/QuartzCore.h>
- // Uncomment viewDidLoad and add the following lines
- self.view.layer.backgroundColor = [UIColor orangeColor].CGColor;
- self.view.layer.cornerRadius = 20.0;
- self.view.layer.frame = CGRectInset(self.view.layer.frame, 20, 20);
层和子层
- CALayer *sublayer = [CALayer layer];
- sublayer.backgroundColor = [UIColor blueColor].CGColor;
- sublayer.shadowOffset = CGSizeMake(0, 3);
- sublayer.shadowRadius = 5.0;
- sublayer.shadowColor = [UIColor blackColor].CGColor;
- sublayer.shadowOpacity = 0.8;
- sublayer.frame = CGRectMake(30, 30, 128, 192);
- [self.view.layer addSublayer:sublayer];
设置层的图像内容
- sublayer.contents = (id) [UIImage imageNamed:@"BattleMapSplashScreen.jpg"].CGImage;
- sublayer.borderColor = [UIColor blackColor].CGColor;
- sublayer.borderWidth = 2.0;
圆角半径和图片内容的注意点
- CALayer *sublayer = [CALayer layer];
- sublayer.backgroundColor = [UIColor blueColor].CGColor;
- sublayer.shadowOffset = CGSizeMake(0, 3);
- sublayer.shadowRadius = 5.0;
- sublayer.shadowColor = [UIColor blackColor].CGColor;
- sublayer.shadowOpacity = 0.8;
- sublayer.frame = CGRectMake(30, 30, 128, 192);
- sublayer.borderColor = [UIColor blackColor].CGColor;
- sublayer.borderWidth = 2.0;
- sublayer.cornerRadius = 10.0;
- [self.view.layer addSublayer:sublayer];
- CALayer *imageLayer = [CALayer layer];
- imageLayer.frame = sublayer.bounds;
- imageLayer.cornerRadius = 10.0;
- imageLayer.contents = (id) [UIImage imageNamed:@"BattleMapSplashScreen.jpg"].CGImage;
- imageLayer.masksToBounds = YES;
- [sublayer addSublayer:imageLayer];
层和自定义绘画内容
- CALayer *customDrawn = [CALayer layer];
- customDrawn.delegate = self;
- customDrawn.backgroundColor = [UIColor greenColor].CGColor;
- customDrawn.frame = CGRectMake(30, 250, 128, 40);
- customDrawn.shadowOffset = CGSizeMake(0, 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];
- [customDrawn setNeedsDisplay];
- 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;
- 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);
- }
- - (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)context {
- CGColorRef bgColor = [UIColor colorWithHue:0.6 saturation:1.0 brightness:1.0 alpha:1.0].CGColor;
- CGContextSetFillColorWithColor(context, bgColor);
- CGContextFillRect(context, layer.bounds);
- static const CGPatternCallbacks callbacks = { 0, &MyDrawColoredPattern, NULL };
- CGContextSaveGState(context);
- CGColorSpaceRef patternSpace = CGColorSpaceCreatePattern(NULL);
- CGContextSetFillColorSpace(context, patternSpace);
- CGColorSpaceRelease(patternSpace);
- CGPatternRef pattern = CGPatternCreate(NULL,
- layer.bounds,
- CGAffineTransformIdentity,
- 24,
- 24,
- kCGPatternTilingConstantSpacing,
- true,
- &callbacks);
- CGFloat alpha = 1.0;
- CGContextSetFillPattern(context, pattern, &alpha);
- CGPatternRelease(pattern);
- CGContextFillRect(context, layer.bounds);
- CGContextRestoreGState(context);
- }
将来
这篇关于CALayer的简单使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!