CALayer -- 备忘
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 -- 备忘的更多相关文章
- Objective-C教程备忘单
终极版本的Objective-C教程备忘单帮助你进行iOS开发. 想开始创建你的第一个iOS应用程序么?那么看一下这篇很棒的教程吧:Create your first iOS 7 Hello Worl ...
- GIS部分理论知识备忘随笔
文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1.高斯克吕格投影带换算 某坐标的经度为112度,其投影的6度带和3度带 ...
- python序列,字典备忘
初识python备忘: 序列:列表,字符串,元组len(d),d[id],del d[id],data in d函数:cmp(x,y),len(seq),list(seq)根据字符串创建列表,max( ...
- Vi命令备忘
备忘 Ctrl+u:向文件首翻半屏: Ctrl+d:向文件尾翻半屏: Ctrl+f:向文件尾翻一屏: Ctrl+b:向文件首翻一屏: Esc:从编辑模式切换到命令模式: ZZ:命令模式下保存当前文件所 ...
- ExtJs4常用配置方法备忘
viewport布局常用属性 new Ext.Viewport({ layout: "border", renderTo: Ext.getBody(), defaults: { b ...
- [备忘] Automatically reset Windows Update components
这两天遇到Windows 10的更新问题,官方有一个小工具,可以用来修复Windows Update的问题,备忘如下 https://support.microsoft.com/en-us/kb/97 ...
- ECMAScript 5(ES5)中bind方法简介备忘
一直以来对和this有关的东西模糊不清,譬如call.apply等等.这次看到一个和bind有关的笔试题,故记此文以备忘. bind和call以及apply一样,都是可以改变上下文的this指向的.不 ...
- MFC通过txt查找文件并进行复制-备忘
MFC基于对话框的Demo txt中每行一个23位的卡号. 文件夹中包含以卡号命名的图像文件.(fpt或者bmp文件) 要求遍历文件夹,找到txt中卡号所对应的图像文件,并复制出来. VC6.0写的. ...
- php 相关模块备忘
在安装php的时候,不管是编译安装: ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc -- ...
随机推荐
- Linux--------------安装nginx ftp
阿里云服务器ECS centos7.2搭建nginx环境以及负载均衡 http://blog.csdn.net/ul646691993/article/details/52104082
- 从源码角度深入理解LayoutInflater
关于LayoutInflater,在开发中经常会遇到,特别是在使用ListView的时候,这个几乎是必不可少.今天我们就一起来探讨LayoutInflater的工作原理. 一般情况下,有两种方式获得一 ...
- Mysql 半同步复制配置
以下是配置和监控半同步复制: 1. 半同步复制功能以plugin的方式接入MySQL,需要在主库与从库两端同时开启半同步的支持,具体配置如下: On the master mysql> INST ...
- javascript中的内置对象
2015.12.1 javascript中的内置对象 复习: 1.js中的内置函数 alert prompt write parseInt parseFloat eval isNaN document ...
- .Net之美读书系列(一):委托与事件
开启新的读书之旅,这次读的书为<.Net之美:.Net关键技术深入解析>. 我是选择性阅读的,把一些自己觉得容易忘记的,或者比较重要的知识点记录下来,以便以后能方便呢查阅. 尊重书本原作者 ...
- Windows 7中,用Visual Studio开发WPF应用程序,实现从Windows Explorer中拖拽文件到应用程序,始终显示“无法拖放”符号问题解决方案
Are you running your application or Visual Studio that hosts the app under administrative privilege? ...
- Jquery CheckBox复选框 全选/取消全选 最佳实现方式 参考案例
<input id="chkAll" type="checkbox" />全选/取消全选</div> <asp:Repeater ...
- RAC分解步骤之一,在oracle linux 4u4上安装oracle 10.2.0.1.0操作日志
练习oracle的rac组建过程,第一步,先练习4u4上安装oracle 10.2.0.1.0.直接安装rac,有些难度.从简单的做起.总RAC步骤,参照小布老师的RAC组建. 1. 启动vc,登陆v ...
- Mac 下显示隐藏文件
将下面的命令粘贴进终端,按提示操作即可(可能需要输入电脑密码) 显示:defaults write com.apple.finder AppleShowAllFiles -bool true 隐藏:d ...
- 一、VSTO概述
一.什么是VSTO? VSTO = Visual Studo Tools for Office,是.net平台下的Office开发技术.相对于传统的VBA(Visual Basic Applicati ...