CALayer

layer是层,每个view上都会最少有一个layer,view上的可视化内容其实都是层。

CALayer展示实例

        let customView = UIView(frame: CGRectMake(0, 0, 100, 100))
customView.center = view.center
view.addSubview(customView) let layer = customView.layer
layer.backgroundColor = UIColor.brownColor().CGColor
layer.masksToBounds = true
layer.cornerRadius = 50.0
layer.borderWidth = 2
layer.borderColor = UIColor.blackColor().CGColor
layer.shadowColor = UIColor.blackColor().CGColor
layer.shadowOpacity = 0.8
layer.shadowOffset = CGSizeMake(15, 5)
layer.contents = UIImage(named: "icon.png")?.CGImage

上面是一些常用的属性,更多属性请自行参考:

  • backgroundColor:背景颜色
  • masksToBounds:将超过layer展示范围的内容剪掉
  • cornerRadius:设置圆角
  • borderWidth:边框宽度
  • borderColor:边框颜色
  • shadowColor:阴影颜色
  • shadowOpacity:阴影的透明度 0.0 ~ 1.0
  • shadowOffset:阴影的位置 (masksToBounds为true时 不显示阴影)
  • contents:设置图层上的内容

CALayer的显式动画和隐式动画

        // 先把layer添加到主图层上
let customLayer = CALayer()
customLayer.bounds = CGRectMake(0.0, 0.0, 100, 100)
customLayer.position = CGPointMake(150, 200)
customLayer.backgroundColor = UIColor.blackColor().CGColor
view.layer.addSublayer(customLayer) self.customLayer = customLayer override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { // 开启事务
CATransaction.begin()
// 设置动画时间
CATransaction.setValue(NSNumber(double: 2.0), forKey: kCATransactionAnimationDuration)
customLayer.position = CGPointMake(50.0, 50.0)
customLayer.backgroundColor = UIColor.redColor().CGColor
customLayer.cornerRadius = 50.0
// 提交事务
CATransaction.commit()
}

上面的例子是执行显示动画,这样一些不具有隐式动画的属性被修改后也可以做出动画效果,比如:cornerRadius。

具有隐式动画的属性在修改值的时候不需要做任何动画处理就会出现动画效果。

一些具有隐式动画的属性(更多属性请看官方文档或进入文件查看,都会有标注的):

  • position
  • backgroundColor
  • bounds

如果修改这些属性不想执行隐式动画可以这样做

        // 开启事务
CATransaction.begin()
// 设置是否执行动画
CATransaction.setValue(kCFBooleanTrue, forKey: kCATransactionDisableActions)
customLayer.position = CGPointMake(50.0, 50.0)
customLayer.backgroundColor = UIColor.redColor().CGColor
// 提交事务
CATransaction.commit()

CATextLayer

        let textLayer = CATextLayer()
textLayer.bounds = CGRectMake(0, 0, 300, 100)
textLayer.position = CGPointMake(10, 200)
textLayer.anchorPoint = CGPointMake(0.0, 0.0)
textLayer.backgroundColor = UIColor.yellowColor().CGColor
textLayer.string = "一些字符串,一些字符串,一些字符串,一些字符串,"
textLayer.cornerRadius = 10
textLayer.alignmentMode = kCAAlignmentCenter
textLayer.wrapped = false
textLayer.truncationMode = kCATruncationEnd
textLayer.contentsScale = UIScreen.mainScreen().scale
let font = UIFont(name: "Helvetica-Bold", size: 13)
let fontRef = CGFontCreateWithFontName(font!.fontName)
textLayer.font = fontRef!
textLayer.fontSize = font!.pointSize
textLayer.foregroundColor = UIColor.blackColor().CGColor
view.layer.addSublayer(textLayer) self.textLayer = textLayer

一些常用属性(更多参照官方文档):

  • string:要显示的字符串,可以是富文本,有隐式动画。
  • alignmentMode:排列模式(居中,靠左,靠右),有隐式动画。
  • wrapped:包裹字符串(是否换行显示)。
  • trancationMode:裁剪字符串模式,当超出范围时指定如何裁剪字符串。
  • contentsScale:内容缩放,需要设置为屏幕的scale,否则可能会出现字体模糊现象。
  • font:字体,有隐式动画。
  • fontSize:字体大小,有隐式动画。
  • foregroundColor:字体颜色,有隐式动画。

CALayer -- 备忘的更多相关文章

  1. Objective-C教程备忘单

    终极版本的Objective-C教程备忘单帮助你进行iOS开发. 想开始创建你的第一个iOS应用程序么?那么看一下这篇很棒的教程吧:Create your first iOS 7 Hello Worl ...

  2. GIS部分理论知识备忘随笔

    文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1.高斯克吕格投影带换算 某坐标的经度为112度,其投影的6度带和3度带 ...

  3. python序列,字典备忘

    初识python备忘: 序列:列表,字符串,元组len(d),d[id],del d[id],data in d函数:cmp(x,y),len(seq),list(seq)根据字符串创建列表,max( ...

  4. Vi命令备忘

    备忘 Ctrl+u:向文件首翻半屏: Ctrl+d:向文件尾翻半屏: Ctrl+f:向文件尾翻一屏: Ctrl+b:向文件首翻一屏: Esc:从编辑模式切换到命令模式: ZZ:命令模式下保存当前文件所 ...

  5. ExtJs4常用配置方法备忘

    viewport布局常用属性 new Ext.Viewport({ layout: "border", renderTo: Ext.getBody(), defaults: { b ...

  6. [备忘] Automatically reset Windows Update components

    这两天遇到Windows 10的更新问题,官方有一个小工具,可以用来修复Windows Update的问题,备忘如下 https://support.microsoft.com/en-us/kb/97 ...

  7. ECMAScript 5(ES5)中bind方法简介备忘

    一直以来对和this有关的东西模糊不清,譬如call.apply等等.这次看到一个和bind有关的笔试题,故记此文以备忘. bind和call以及apply一样,都是可以改变上下文的this指向的.不 ...

  8. MFC通过txt查找文件并进行复制-备忘

    MFC基于对话框的Demo txt中每行一个23位的卡号. 文件夹中包含以卡号命名的图像文件.(fpt或者bmp文件) 要求遍历文件夹,找到txt中卡号所对应的图像文件,并复制出来. VC6.0写的. ...

  9. php 相关模块备忘

    在安装php的时候,不管是编译安装: ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc -- ...

随机推荐

  1. [AngularJS] angular-formly: Default Options

    angular-formly allows you to keep your forms as DRY as possible. TheoptionsTypes property is one way ...

  2. systemtap 列出所有linux 内核模块与相关函数1

    阿里云主机 [root@monitor klvl]# uname -aLinux monitor 2.6.32-431.23.3.el6.x86_64 #1 SMP Thu Jul 31 17:20: ...

  3. Android(java)学习笔记161:Framework运行环境之启动SystemServer进程

          SystemServer进程是zygote孵化出的第一个进程,该进程是从ZygoteInit.java的main函数中调用startSystemServer()开始的.与启动普通进程的差别 ...

  4. JVM笔记5:Class文件结构

    Class文件是一组以8位字节为基础单位的二进制流,包含多个数据项目(数据项目的顺序,占用的字节数均由规范定义),各个数据项目严格按照顺序紧凑的排列在Class文件中,不包含任何分隔符,使得整个Cla ...

  5. C#后台验证身份证号码的一个方法

    简单的调用了正则表达式进行简单的验证,记下来留着以后备用 if ((!Regex.IsMatch(txtID.Text, @"^(^\d{15}$|^\d{18}$|^\d{17}(\d|X ...

  6. JBPM WEB CONSOLE安装实录

    http://www.blogjava.net/paulwong/archive/2009/03/13/259551.html JBPM WEB CONSOLE是一个B/S端的,能管理JBPM的流程和 ...

  7. CSS样式表介绍

    一.    CSS中的样式选择 1)内样式(内联样式) style=””; 2)内嵌样式 <style type="text/css"></style> 3 ...

  8. MVC中,查询以异步呈现,分页不用异步的解决方案

    MVC中,查询以异步呈现,分页不用异步的解决方案 这种需求,用一个ASPX页面和一个ASCX分部视图就可以解决了,ASPX提供对ASCX的引用,ASCX显示列表信息,ASPX主页面提供查询功能 < ...

  9. 类库探源——System.Configuration 配置信息处理

    按照MSDN描述 System.Configuration 命名空间 包含处理配置信息的类型 本篇文章主要两方面的内容 1. 如何使用ConfigurationManager 读取AppSetting ...

  10. Cesium的api之关于viewer(二)

    1.构建一个viewer,如下创建:options的参数根据实际情况,进行设定 var viewer = new Cesium.Viewer('cesiumContainer', { //Start ...