IOS Core Animation Advanced Techniques的学习笔记(二)
- - (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
- {
- CGFloat width = 10.0f;
- //draw a thick red circle
- CGContextSetLineWidth(ctx, width);
- CGContextSetStrokeColorWithColor(ctx, [UIColor redColor].CGColor);
- CGRect rect = CGRectMake(layer.bounds.origin.x+width/2, layer.bounds.origin.y+width/2, layer.bounds.size.width-width, layer.bounds.size.height-width);
- CGContextStrokeEllipseInRect(ctx, rect);
- }
接上次,修改了一下,呵呵,看着舒服多了
由此可见线宽的扩展方式是同时向两边扩展的。
第三章:Layer Geometry
“Let no one unversed in geometry enter here.”
看到作者的信心了吧,你有了吗?
UIView的布局属性有frame,bounds和center,CALayer同样有3个对应frame,bounds和position。如图:
需要注意的是,frame不是一个独立的属性,它是由bounds,position,transform等其他属性计算而来,所以改变frame会影响其他属性,反之改变其他属性也会影响frame。如果我在前面的例子里增加一行改变frame的代码你会看到如下结果
- - (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
- {
- CGFloat width = 10.0f;
- //draw a thick red circle
- CGContextSetLineWidth(ctx, width);
- CGContextSetStrokeColorWithColor(ctx, [UIColor redColor].CGColor);
- CGRect rect = CGRectMake(layer.bounds.origin.x+width/2, layer.bounds.origin.y+width/2, layer.bounds.size.width-width, layer.bounds.size.height-width);
- CGContextStrokeEllipseInRect(ctx, rect);
- layer.frame = CGRectMake(10.0f, 10.0f, 200.0f, 200.0f);
- }
还有需要注意在操作transform时,如旋转时,frame会重新计算,此时frame的宽高不再和bounds比配。如图
下面再说说anchorPoint这个属性,很多人对于使用此属性可能会迷惑,到底是怎么设置的,先看张图
anchorPoint从{0.5, 0.5}变为{0, 0},layer向右下偏移了,下面大家可以用一下做个试验
还是使用上面的例子,在- (void)viewDidLoad的[blueLayerdisplay];前增加
- NSLog(@"%@", NSStringFromCGPoint(blueLayer.anchorPoint));
- NSLog(@"%@", NSStringFromCGPoint(blueLayer.position));
是为了记录变化前的值,接着在- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx中
增加对anchorPoint的设置,自已可以任意设置看看结果
- - (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
- {
- CGFloat width = 10.0f;
- //draw a thick red circle
- CGContextSetLineWidth(ctx, width);
- CGContextSetStrokeColorWithColor(ctx, [UIColor redColor].CGColor);
- CGRect rect = CGRectMake(layer.bounds.origin.x+width/2, layer.bounds.origin.y+width/2, layer.bounds.size.width-width, layer.bounds.size.height-width);
- CGContextStrokeEllipseInRect(ctx, rect);
- layer.anchorPoint = CGPointMake(0, 0);
- NSLog(@"%@", NSStringFromCGPoint(layer.position));
- }
我们可以得出结论,anchorPoint可以简单地看作是设置左上顶点相对于中心position的位置,
计数以左上顶点到position的距离与宽高的比例,X轴方向正为左移负为右移,Y轴方向正为上移负为下移。
下面我们用例子来看看具体的应用,具体代码请查看例子3.1
源码在这里下载:http://www.informit.com/title/9780133440751
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- //start timer
- self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0
- target:self
- selector:@selector(tick)
- userInfo:nil
- repeats:YES];
- //set initial hand positions
- [self tick];
- }
- - (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]];
- //calculate hour hand angle
- CGFloat hourAngle = (components.hour / 12.0) * M_PI * 2.0;
- //calculate minute hand angle
- CGFloat minuteAngle = (components.minute / 60.0) * M_PI * 2.0;
- //calculate second hand angle
- CGFloat secondAngle = (components.second / 60.0) * M_PI * 2.0;
- //rotate hands
- self.hourHand.transform = CGAffineTransformMakeRotation(hourAngle);
- self.minuteHand.transform = CGAffineTransformMakeRotation(minuteAngle);
- self.secondHand.transform = CGAffineTransformMakeRotation(secondAngle);
- }
运行看看结果
发现什么了没有?CGAffineTransformMakeRotation以View的center点在旋转
再看例子3.2,修改代码
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- //adjust anchor points
- self.secondHand.layer.anchorPoint = CGPointMake(0.5f, 0.9f); // 为什么不是1.0f?
- self.minuteHand.layer.anchorPoint = CGPointMake(0.5f, 0.9f);
- self.hourHand.layer.anchorPoint = CGPointMake(0.5f, 0.9f);
- //start timer
- self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0
- target:self
- selector:@selector(tick)
- userInfo:nil
- repeats:YES];
- //set initial hand positions
- [self tick];
- }
看结果
CGAffineTransformMakeRotation以layer的position点在旋转
相对坐标的转换函数,UIView使用:
- (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view;
- (CGPoint)convertPoint:(CGPoint)point fromView:(UIView *)view;
- (CGRect)convertRect:(CGRect)rect toView:(UIView *)view;
- (CGRect)convertRect:(CGRect)rect fromView:(UIView *)view;
CALayer使用:
- (CGPoint)convertPoint:(CGPoint)p fromLayer:(CALayer *)l;
- (CGPoint)convertPoint:(CGPoint)p toLayer:(CALayer *)l;
- (CGRect)convertRect:(CGRect)r fromLayer:(CALayer *)l;
- (CGRect)convertRect:(CGRect)r toLayer:(CALayer *)l;
注意:直到OS X 10.8才出现了geometryFlipped属性,该属性可以改变默认图层y坐标的方向。当翻转变换被调用时,使用该属性来调整图层的方向有的时候是必需的。如果父视图使用了翻转变换,它的子视图内容(以及它对应的图层)将经常被颠倒。在这种情况下,设置子图层的geometryFlipped属性为YES是一种修正该问题最简单的方法。在OS X 10.8及以上版本,AppKit负责管理该属性,你不应该更改它。对于iOS app,不推荐使用geometryFlipped属性。
“This is a BOOL value that determines whether the geometry of a layer is vertically flipped with respect to its superlayer. Setting this property to YES for a layer on iOS means that its sublayers will be flipped vertically and will be positioned relative to the bottom of its bounds rather than the top as normal (as will all of their sublayers, and so on, unless they also have YES for their geometryFlipped property).”
注意的是geometryFlipped只对sublayers起作用,只到有sub也设置了YES才恢复,有点负负得正的意思
修改例子3.2代码
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- //adjust anchor points
- self.view.layer.geometryFlipped = YES;
- self.secondHand.layer.anchorPoint = CGPointMake(0.5f, 0.9f);
- self.minuteHand.layer.anchorPoint = CGPointMake(0.5f, 0.9f);
- self.hourHand.layer.anchorPoint = CGPointMake(0.5f, 0.9f);
- //start timer
- self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0
- target:self
- selector:@selector(tick)
- userInfo:nil
- repeats:YES];
- //set initial hand positions
- [self tick];
- }
自己运行看看,指针反着转了。
再修改看看的geometryFlipped sublayers的作用域,呵呵暂且这么说
首先增加一个view作为secondHand的父view,修改代码
增加
- @property (weak, nonatomic) IBOutlet UIView *sView;
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- //adjust anchor points
- self.view.layer.geometryFlipped = YES;
- self.sView.layer.geometryFlipped = YES;
- self.secondHand.layer.anchorPoint = CGPointMake(0.5f, 0.9f);
- self.minuteHand.layer.anchorPoint = CGPointMake(0.5f, 0.9f);
- self.hourHand.layer.anchorPoint = CGPointMake(0.5f, 0.9f);
- //start timer
- self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0
- target:self
- selector:@selector(tick)
- userInfo:nil
- repeats:YES];
- //set initial hand positions
- [self tick];
- }
看结果
秒针正常了
我们以前接触的UIView是2维坐标,但现在CAlayer确实3维坐标,增加了Z轴, zPosition 和 anchorPointZ
看例子3.3,zPosition默认值为0,当设置self.greenView.layer.zPosition大于0时,greenView将遮盖redView
运行结果
例子3.4和3.5是对
- /* Returns the farthest descendant of the layer containing point 'p'.
- * Siblings are searched in top-to-bottom order. 'p' is defined to be
- * in the coordinate space of the receiver's nearest ancestor that
- * isn't a CATransformLayer (transform layers don't have a 2D
- * coordinate space in which the point could be specified). */
- - (CALayer *)hitTest:(CGPoint)p;
- /* Returns true if the bounds of the layer contains point 'p'. */
- - (BOOL)containsPoint:(CGPoint)p;
两个函数的,自己去看
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的学习笔记(三)
第四章:Visual Effects Rounded Corners 例子4.1 cornerRadius 源码在这里下载:http://www.informit.com/title/978013 ...
- 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 ...
随机推荐
- 全球IP分布表
24.192.0.0 24.195.255.255 亚洲 61.0.0.0 61.255.255.255 亚洲 61.8.0.0 61.8.31.255 澳大利亚 61.128.0.0 61.143. ...
- 准备阶段-maven项目构建
依据我现阶段对maven的了解,具使用POM管理项目和强大的repository资源管理库支持. 在项目建立初期,对网站的可拓展.高并发.易于管理做了评估.最终使用Maven 管理该项目 . 如下是m ...
- CPU过高的排查方法
一个应用占用CPU很高,除了确实是计算密集型应用之外,通常原因都是出现了死循环. (友情提示:本博文章欢迎转载,但请注明出处:hankchen,http://www.blogjava.net/hank ...
- 狗汪汪玩转无线电 -- GPS Hacking
狗汪汪玩转无线电 -- GPS Hacking Kevin2600 · 2015/12/09 10:12 0x00 序 GPS Hacking 在过去几年的安全会议上一直都是很受关注的议题. 但往往因 ...
- md语法之行内代码和代码片
md语法之行内代码和代码片 比如说要在行内写上一句或者半句代码(代码的意思就是某种脚本语言), 用撇号围起来就可以了. 比如: import pandas as pd 写代码片(单独的一块脚本语言)的 ...
- linux diff命令
diff 命令是 linux上非常重要的工具,用于比较文件的内容,特别是比较两个版本不同的文件以找到改动的地方.diff在命令行中打印每一个行的改动.最新版本的diff还支持二进制文件.diff程序的 ...
- ubuntu16.04中将python3设置为默认
直接执行这两个命令即可: sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 100 sudo upd ...
- C#获取CSV文件内容对逗号和引号分隔的处理
我们知道,使用excel工具保存成csv文件时有几个规则: 1.每一行的单元格内容之间用逗号分隔. 2.如果单元格的内容本身有逗号,这个单元格的内容将会用引号包含. 3.如果单元格的内容本身有引号 1 ...
- haligong2016
A 采用递推的方法,由于要到达棋盘上的一个点,只能从左边或者上边过来,根据加法原则,到达某一点的路径数目,就等于到达其相邻的上点和左点的路径数目的总和.所有海盗能达到的点将其路径数置为0即可. #in ...
- SPSS数据分析—典型相关分析
我们已经知道,两个随机变量间的相关关系可以用简单相关系数表示,一个随机变量和多个随机变量的相关关系可以用复相关系数表示,而如果需要研究多个随机变量和多个随机变量间的相关关系,则需要使用典型相关分析. ...