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 -- ...
随机推荐
- 嵌入式Qt4.7.1安装详解
嵌入式Qt 4.7.1安装移植过程详解环境:Ubuntu 12.04VMware 9.0 qt软件包:qt-everywhere-opensource-src-4.7.1.tar.gz (飞凌自带) ...
- 音频播放AVFoundation框架
一.系统声音 ios应用中的提醒声音.游戏背景音乐等.可以播放的格式有CAF.AIF.WAV. 系统声音服务提供了一个API,但是没有操作声音和控制音量的功能,因此如果为多媒体或者游戏创建专门的声音, ...
- DirectShow初探
filtergraphmanagernullmicrosoftdirect3d 可能到现在为止,还没有哪个玩过游戏的人没有接触过Microsoft的DirectX的.因为现今大多数的游戏都是用Dire ...
- C#中的字符串
1. 值类型与引用类型比较 classProgram { staticvoid Main() { int b = a; //将a的副本给变量b b = 10; Console.WriteLi ...
- day-3
/* 快noip了!!!感觉还有好多事要做 上午考试 原题没做 自己找了套题 挺水的 T1模拟(然而没认真读题 90) T2 dp+简单优化 数据有点问题 T3 暴力状丫 然而写丑了 60分的要两秒多 ...
- js 注册手机摇动事件
var rockModule = (function () { //监听手机摇动事件 if (window.DeviceMotionEvent) { window.addEventListener(' ...
- 2.redis.3.2 下载,安装、配置、使用 - 2
上篇简单介绍了 下载,安装,测试,现在直接使用了,看结果 使用的redis服务便是,上篇临时搭建的简易服务,,注意,说的是简易,因为它只是一个单点的“玩具”: 临时在项目登录的时候模拟了一下,这里使用 ...
- OC - 1.面向过程和面向对象的思想对比
一.面向过程 1> 思想 面向过程是一种以过程为中心的最基础编程思想,不支持面向对象的特性. 面向过程是一种模块化程序设计方法 2> 开发方法 面向过程的开发方法是以过程(也可以说是模块) ...
- javascript document对象 第21节
<html> <head> <title>DOM对象</title> <style type="text/css"> t ...
- Oracle 10g 默认安装带来的用户名/密码
ORACLE数据库创建的时候,创建了一系列默认的用户和表空间 Oracle 10g 默认安装带来的用户名/密码 Username Password Description See Also CTXSY ...