IOS Core Animation Advanced Techniques的学习笔记(三)
第四章:Visual Effects
Rounded Corners
例子4.1 cornerRadius
源码在这里下载:http://www.informit.com/title/9780133440751
- #import "ViewController.h"
- #import <QuartzCore/QuartzCore.h>
- @interface ViewController ()
- @property (nonatomic, weak) IBOutlet UIView *layerView1;
- @property (nonatomic, weak) IBOutlet UIView *layerView2;
- @end
- @implementation ViewController
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- //set the corner radius on our layers
- self.layerView1.layer.cornerRadius = 20.0f;
- self.layerView2.layer.cornerRadius = 20.0f;
- //enable clipping on the second layer
- self.layerView2.layer.masksToBounds = YES;
- }
- @end
稍微修改一下
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- //set the corner radius on our layers
- self.layerView1.layer.cornerRadius = 20.0f;
- self.layerView2.layer.cornerRadius = 20.0f;
- self.layerView1.clipsToBounds = YES;
- //enable clipping on the second layer
- self.layerView2.layer.masksToBounds = YES;
- }
前面讲过了,UIView的clipsToBounds的函数等同于masksToBounds
Layer Borders
例子4.2 borderWidth
- @interface ViewController ()
- @property (nonatomic, weak) IBOutlet UIView *layerView1;
- @property (nonatomic, weak) IBOutlet UIView *layerView2;
- @end
- @implementation ViewController
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- //set the corner radius on our layers
- self.layerView1.layer.cornerRadius = 20.0f;
- self.layerView2.layer.cornerRadius = 20.0f;
- //add a border to our layers
- self.layerView1.layer.borderWidth = 5.0f;
- self.layerView2.layer.borderWidth = 5.0f;
- //enable clipping on the second layer
- self.layerView2.layer.masksToBounds = YES;
- }
- @end
修改代码 borderColor
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- //set the corner radius on our layers
- self.layerView1.layer.cornerRadius = 20.0f;
- self.layerView2.layer.cornerRadius = 20.0f;
- //add a border to our layers
- self.layerView1.layer.borderWidth = 5.0f;
- self.layerView1.layer.borderColor = [UIColor brownColor].CGColor;
- self.layerView2.layer.borderWidth = 5.0f;
- //enable clipping on the second layer
- self.layerView2.layer.masksToBounds = YES;
- }
再做个试验,修改代码
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- //set the corner radius on our layers
- //self.layerView1.layer.cornerRadius = 20.0f;
- self.layerView2.layer.cornerRadius = 20.0f;
- //add a border to our layers
- self.layerView1.layer.borderWidth = 5.0f;
- self.layerView1.layer.borderColor = [UIColor brownColor].CGColor;
- self.layerView2.layer.borderWidth = 5.0f;
- //enable clipping on the second layer
- self.layerView2.layer.masksToBounds = YES;
- }
没有看到红色
再修改
看结果
验证borderWidth是往内部画的,和使用CGContextStrokeEllipseInRect画圆时的方式不同
Drop Shadows & Shadow Clipping
先修改例子2.2
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- //load an image
- UIImage *image = [UIImage imageNamed:@"Snowman.png"];
- self.layerView.backgroundColor = [UIColor clearColor];
- //add it directly to our view's layer
- self.layerView.layer.contents = (__bridge id)image.CGImage;
- //center the image
- self.layerView.layer.contentsGravity = kCAGravityCenter;
- //set the contentsScale to match screen
- self.layerView.layer.contentsScale = image.scale;
- self.layerView.layer.shadowOpacity = 0.3;
- self.layerView.layer.shadowOffset = CGSizeMake(10, 20);
- //clip the snowman to fit his bounds
- //self.layerView.layer.masksToBounds = YES;
- }
继续
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- //load an image
- UIImage *image = [UIImage imageNamed:@"Snowman.png"];
- //self.layerView.backgroundColor = [UIColor clearColor];
- //add it directly to our view's layer
- self.layerView.layer.contents = (__bridge id)image.CGImage;
- //center the image
- self.layerView.layer.contentsGravity = kCAGravityCenter;
- //set the contentsScale to match screen
- self.layerView.layer.contentsScale = image.scale;
- self.layerView.layer.shadowOpacity = 0.3;
- self.layerView.layer.shadowOffset = CGSizeMake(10, 20);
- //clip the snowman to fit his bounds
- //self.layerView.layer.masksToBounds = YES;
- }
再改
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- //load an image
- UIImage *image = [UIImage imageNamed:@"Snowman.png"];
- self.layerView.backgroundColor = [UIColor clearColor];
- //add it directly to our view's layer
- self.layerView.layer.contents = (__bridge id)image.CGImage;
- //center the image
- self.layerView.layer.contentsGravity = kCAGravityCenter;
- //set the contentsScale to match screen
- self.layerView.layer.contentsScale = image.scale;
- self.layerView.layer.shadowOpacity = 0.3;
- self.layerView.layer.shadowOffset = CGSizeMake(10, 20);
- //clip the snowman to fit his bounds
- self.layerView.layer.masksToBounds = YES;
- }
再改
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- //load an image
- UIImage *image = [UIImage imageNamed:@"Snowman.png"];
- //self.layerView.backgroundColor = [UIColor clearColor];
- //add it directly to our view's layer
- self.layerView.layer.contents = (__bridge id)image.CGImage;
- //center the image
- self.layerView.layer.contentsGravity = kCAGravityCenter;
- //set the contentsScale to match screen
- self.layerView.layer.contentsScale = image.scale;
- self.layerView.layer.shadowOpacity = 0.3;
- self.layerView.layer.shadowOffset = CGSizeMake(10, 20);
- //clip the snowman to fit his bounds
- self.layerView.layer.masksToBounds = YES;
- }
shadow是根据layer实际显示的内容绘制的
再看看例子4.3去体会一下
源码在这里下载:http://www.informit.com/title/9780133440751
The shadowPath Property
例子4.4
- @interface ViewController ()
- @property (nonatomic, weak) IBOutlet UIView *layerView1;
- @property (nonatomic, weak) IBOutlet UIView *layerView2;
- @end
- @implementation ViewController
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- //enable layer shadows
- self.layerView1.layer.shadowOpacity = 0.5f;
- self.layerView2.layer.shadowOpacity = 0.5f;
- //create a square shadow
- CGMutablePathRef squarePath = CGPathCreateMutable();
- CGPathAddRect(squarePath, NULL, self.layerView1.bounds);
- self.layerView1.layer.shadowPath = squarePath;
- CGPathRelease(squarePath);
- //create a circular shadow
- CGMutablePathRef circlePath = CGPathCreateMutable();
- CGPathAddEllipseInRect(circlePath, NULL, self.layerView2.bounds);
- self.layerView2.layer.shadowPath = circlePath;
- CGPathRelease(circlePath);
- }
Layer Masking
例子4.5
- @interface ViewController ()
- @property (nonatomic, weak) IBOutlet UIImageView *imageView;
- @end
- @implementation ViewController
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- //create mask layer
- CALayer *maskLayer = [CALayer layer];
- maskLayer.frame = self.imageView.bounds;
- UIImage *maskImage = [UIImage imageNamed:@"Cone.png"];
- maskLayer.contents = (__bridge id)maskImage.CGImage;
- //apply mask to image layer
- self.imageView.layer.mask = maskLayer;
- }
- @end
Scaling Filters
minificationFilter和magnificationFilter属性
这两个属性主要是设置layer的‘contents’数据缩放拉伸时的描绘方式,minificationFilter用于缩小,magnificationFilter用于放大
默认值都是kCAFilterLinear即‘linear’
有3中设置:kCAFilterLinear,kCAFilterNearest,kCAFilterTrilinear
kCAFilterLinear:默认值,缩放平滑,但容易产生模糊效果
kCAFilterTrilinear:基本和kCAFilterLinear相同
kCAFilterNearest:速度快不会产生模糊,但会降低质量并像素化图像
例子4.6,放大图像,设置magnificationFilter
原图
- @interface ViewController ()
- @property (nonatomic, strong) IBOutletCollection(UIView) NSArray *digitViews;
- @property (nonatomic, weak) NSTimer *timer;
- @end
- @implementation ViewController
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- //get spritesheet image
- UIImage *digits = [UIImage imageNamed:@"Digits.png"];
- //set up digit views
- for (UIView *view in self.digitViews)
- {
- //set contents
- view.layer.contents = (__bridge id)digits.CGImage;
- view.layer.contentsRect = CGRectMake(0, 0, 0.1, 1.0);
- view.layer.contentsGravity = kCAGravityResizeAspect;
- //use nearest-neighbor scaling
- view.layer.magnificationFilter = kCAFilterNearest;
- }
- //start timer
- self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0
- target:self
- selector:@selector(tick)
- userInfo:nil
- repeats:YES];
- //set initial clock time
- [self tick];
- }
- - (void)setDigit:(NSInteger)digit forView:(UIView *)view
- {
- //adjust contentsRect to select correct digit
- view.layer.contentsRect = CGRectMake(digit * 0.1, 0, 0.1, 1.0);
- }
- - (void)tick
- {
- //convert time to hours, minutes and seconds
- NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
- NSUInteger units = NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
- NSDateComponents *components = [calendar components:units fromDate:[NSDate date]];
- //set hours
- [self setDigit:components.hour / 10 forView:self.digitViews[0]];
- [self setDigit:components.hour % 10 forView:self.digitViews[1]];
- //set minutes
- [self setDigit:components.minute / 10 forView:self.digitViews[2]];
- [self setDigit:components.minute % 10 forView:self.digitViews[3]];
- //set seconds
- [self setDigit:components.second / 10 forView:self.digitViews[4]];
- [self setDigit:components.second % 10 forView:self.digitViews[5]];
- }
- @end
kCAFilterNearest的效果
注释掉
- //view.layer.magnificationFilter = kCAFilterNearest;
使用用默认kCAFilterLinear效果
明显模糊了
Group Opacity
先看例子4.7:
- @interface ViewController ()
- @property (nonatomic, weak) IBOutlet UIView *containerView;
- @end
- @implementation ViewController
- - (UIButton *)customButton
- {
- //create button
- CGRect frame = CGRectMake(0, 0, 150, 50);
- UIButton *button = [[UIButton alloc] initWithFrame:frame];
- button.backgroundColor = [UIColor whiteColor];
- button.layer.cornerRadius = 10;
- //add label
- frame = CGRectMake(20, 10, 110, 30);
- UILabel *label = [[UILabel alloc] initWithFrame:frame];
- label.text = @"Hello World";
- //label.backgroundColor = [UIColor clearColor];
- label.textAlignment = NSTextAlignmentCenter;
- [button addSubview:label];
- return button;
- }
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- //create opaque button
- UIButton *button1 = [self customButton];
- button1.center = CGPointMake(50, 150);
- [self.containerView addSubview:button1];
- //create translucent button
- UIButton *button2 = [self customButton];
- button2.center = CGPointMake(250, 150);
- button2.alpha = 0.5;
- [self.containerView addSubview:button2];
- //enable rasterization for the translucent button
- //button2.layer.shouldRasterize = YES;
- //button2.layer.rasterizationScale = [UIScreen mainScreen].scale;
- }
- @end
button的背景和其subView label的背景同为白色,
左边的button是不透明的,右边用同样方式创建的button透明度为50%,发现右边的label透明度不同于button
其实很容易发现原因,将button透明度设为50%后,button显示50%自己的颜色和其后面50%的颜色,label在
button上面,label也是50%显示择机的颜色,但后面有已经50%透明的button,还要再显示它的50%,即原
button的25%,重合后为75%,即出现上图效果。
有两种解决方法:
1.在工程的Info.plist文件中,添加UIViewGroupOpacity并设为YES
2.设置layer属性shouldRasterize,设为YES可在设置opacity属性时将layer及其sublayer叠加为一张图像
修改代码,
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- //create opaque button
- UIButton *button1 = [self customButton];
- button1.center = CGPointMake(50, 150);
- [self.containerView addSubview:button1];
- //create translucent button
- UIButton *button2 = [self customButton];
- button2.center = CGPointMake(250, 150);
- button2.alpha = 0.5;
- [self.containerView addSubview:button2];
- //enable rasterization for the translucent button
- button2.layer.shouldRasterize = YES;
- button2.layer.rasterizationScale = [UIScreen mainScreen].scale;
- }
IOS Core Animation Advanced Techniques的学习笔记(三)的更多相关文章
- IOS Core Animation Advanced Techniques的学习笔记(一)
转载. Book Description Publication Date: August 12, 2013 Core Animation is the technology underlying A ...
- IOS Core Animation Advanced Techniques的学习笔记(五)
第六章:Specialized Layers 类别 用途 CAEmitterLayer 用于实现基于Core Animation粒子发射系统.发射器层对象控制粒子的生成和起源 CAGradient ...
- IOS Core Animation Advanced Techniques的学习笔记(四)
第五章:Transforms Affine Transforms CGAffineTransform是二维的 Creating a CGAffineTransform 主要有三种变 ...
- IOS Core Animation Advanced Techniques的学习笔记(二)
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx { CGFloat width = 10.0f; //draw a thi ...
- iOS Core Animation Advanced Techniques
Book Descripter Core Animation is the technology underlying Apple's iOS user interface. By unleashin ...
- 转 iOS Core Animation 动画 入门学习(一)基础
iOS Core Animation 动画 入门学习(一)基础 reference:https://developer.apple.com/library/ios/documentation/Coco ...
- iOS Core Animation 简明系列教程
iOS Core Animation 简明系列教程 看到无数的CA教程,都非常的难懂,各种事务各种图层关系看的人头大.自己就想用通俗的语言翻译给大家听,尽可能准确表达,如果哪里有问题,请您指出我会尽 ...
- iOS - Core Animation 核心动画
1.UIView 动画 具体讲解见 iOS - UIView 动画 2.UIImageView 动画 具体讲解见 iOS - UIImageView 动画 3.CADisplayLink 定时器 具体 ...
- iOS安全些许经验和学习笔记
http://bbs.pediy.com/showthread.php?t=209014 标题: [原创]iOS安全些许经验和学习笔记作者: MonkeyKey时间: 2016-03-30,16:32 ...
随机推荐
- linux开发缩写
1.CONFIG_OF 在一些驱动中经常看到#ifdef CONFIG_OF,这里的OF是Open Firmware. Open Firmware. This was invented long ti ...
- ServerSocket
在网上找ServerSocket看到的解释
- 代码性能优化——task
var t1 = Task.Factory.StartNew(delegate { //代码(查接口.数据库) }); 缺点: 不能使用request( HttpContext.Current.Req ...
- my.cnf
skip-external-locking skip-name-resolve back_log= key_buffer_size=384M max_allowed_packet=4M thread_ ...
- xcode相关配置
Xcode将全部供应配置文件(包括用户手动下载安装的和Xcode自动创建的Team Provisioning Profile)放在目录~/Library/MobileDevice/Provisioni ...
- Cocos2d-x 核心概念 - 游戏循环与调度
每一个游戏程序都有一个循环在不断运行,他是由导演对象来管理和维护. 如果需要场景中的精灵运动起来,我们可以在循环中使用定时器(Scheduler)对精灵等对象的运行进行调度 因为Node中封装了 Sc ...
- Css 备忘知识点
1.Chrome 中文界面下默认会将小于 12px 的文本强制按照 12px 显示,可通过加入 CSS 属性 -webkit-text-size-adjust: none; 2.HTML5的form如 ...
- Redis教程(二):String数据类型
一.概述: 字符串类型是Redis中最为基础的数据存储类型,它在Redis中是二进制安全的,这便意味着该类型可以接受任何格式的数据,如JPEG图像数据或Json对象描述信息等.在Redis中字符串类型 ...
- 转:java怎么用一行代码初始化ArrayList
java怎么用一行代码初始化ArrayList 您可以创建一个工厂方法: public static ArrayList<String> createArrayList(String .. ...
- guardian keytab生成不了
vim /var/kerberos/krb5kdc/kadm5.acl 将*e改成* /etc/init.d/kadmin restart 重启kadmin