CALayer
Layer的基本屬性:
- Layers像Views有subviews一樣,也有sublayers溶褪;
- 當(dāng)你改變Layer的屬性時(shí)币旧,layer會(huì)自動(dòng)動(dòng)畫,你也可以自定義動(dòng)畫時(shí)間猿妈;
- Layers比view更輕量級(jí)吹菱,所有它有更高的性能;
- Layers有很多有用的屬性彭则,接下來看一下這些屬性鳍刷。
// 1
let layer = CALayer()
layer.frame = someView.bounds
// 2
layer.contents = UIImage(named: "star")?.CGImage
layer.contentsGravity = kCAGravityCenter
// 3
layer.magnificationFilter = kCAFilterLinear
layer.geometryFlipped = false
// 4
layer.backgroundColor = UIColor(red: 11/255.0, green: 86/255.0, blue: 14/255.0, alpha: 1.0).CGColor
layer.opacity = 1.0
layer.hidden = false
layer.masksToBounds = false
// 5
layer.cornerRadius = 100.0
layer.borderWidth = 12.0
layer.borderColor = UIColor.whiteColor().CGColor
// 6
layer.shadowOpacity = 0.75
layer.shadowOffset = CGSize(width: 0, height: 3)
layer.shadowRadius = 3.0
someView.layer.addSublayer(layer)
在上面的代碼中:
- 創(chuàng)建一個(gè)layer,給layer設(shè)置frame;
- 設(shè)置layer的content俯抖,居中输瓜,content中的image為CGImage;
- 通過contentGravity設(shè)置圖片時(shí),可以用這個(gè)屬性前痘;
- maskToBounds屬性為false時(shí)layer的size比image小時(shí)凛捏,image不會(huì)被裁掉;
- 設(shè)置cornerRadius為layer的寬度的一半時(shí)芹缔,成了圓形坯癣,borderColor也要用CGColor;
- 創(chuàng)建layer的陰影。
CALayer還有兩個(gè)附加屬性:shouldRasterize,drawsAsynchronously:
- shouldRasterize默認(rèn)為false, 當(dāng)設(shè)置為true時(shí)可以提高性能因?yàn)閘ayer的contents只需要渲染一次最欠,這種適合在屏幕周圍做動(dòng)畫但是不展現(xiàn)在屏幕上的對(duì)象示罗;
- drawsAsynachronously 跟shouldRasterize相反,默認(rèn)也為false, 當(dāng)layer的contents需要不停地重繪時(shí)芝硬,需要設(shè)置為true以提高性能蚜点,例如:用emitter layer不停地渲染動(dòng)畫粒子時(shí)。
CAScrollLayer
UIScrollView 不用CAScrollLayer來工作拌阴,直接改變layer的bounds绍绘,可以對(duì)CAScrollLayer做的是設(shè)置滾動(dòng)模式為水平或者垂直, 可以在程序中告訴它滾動(dòng)到的點(diǎn)或者區(qū)域迟赃,
import UIKit
class ScrollingView: UIView {
// 1
override class func layerClass() -> AnyClass {
return CAScrollLayer.self
}
}
// In CAScrollLayerViewController.swift
import UIKit
class CAScrollLayerViewController: UIViewController {
@IBOutlet weak var scrollingView: ScrollingView!
// 2
var scrollingViewLayer: CAScrollLayer {
return scrollingView.layer as CAScrollLayer
}
override func viewDidLoad() {
super.viewDidLoad()
// 3
scrollingViewLayer.scrollMode = kCAScrollBoth
}
@IBAction func tapRecognized(sender: UITapGestureRecognizer) {
// 4
var newPoint = CGPoint(x: 250, y: 250)
UIView.animateWithDuration(0.3, delay: 0, options: .CurveEaseInOut, animations: {
[unowned self] in
self.scrollingViewLayer.scrollToPoint(newPoint)
}, completion: nil)
}
}
如果想用更輕量級(jí)的滾動(dòng)陪拘,并且用程序設(shè)置滾動(dòng),可以用CAScrollLayer;
如果想用戶自己滾動(dòng)纤壁,最好用UIScrollView;
如果想滾動(dòng)一個(gè)非常大的圖片左刽,考慮用CATiledLayer,在下文中有提到。
CATextLayer
CATextLayer 可以快速的渲染字符串酌媒,它不用 UIFont, 只用CTFontRef欠痴, CGFontRef
// 1
let textLayer = CATextLayer()
textLayer.frame = someView.bounds
// 2
var string = ""
for _ in 1...20 {
string += "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce auctor arcu quis velit congue dictum. "
}
textLayer.string = string
// 3
let fontName: CFStringRef = "Noteworthy-Light"
textLayer.font = CTFontCreateWithName(fontName, fontSize, nil)
// 4
textLayer.foregroundColor = UIColor.darkGrayColor().CGColor
textLayer.wrapped = true
textLayer.alignmentMode = kCAAlignmentLeft
textLayer.contentsScale = UIScreen.mainScreen().scale
someView.layer.addSublayer(textLayer)
- 創(chuàng)建一個(gè)CATextLayer實(shí)例
- 創(chuàng)建一個(gè)重復(fù)字符串
- 把字體賦值給它
- 設(shè)置字體的對(duì)齊方式
所有l(wèi)ayer的類,默認(rèn)為1倍渲染秒咨,當(dāng)加到視圖上時(shí)喇辽,layers自動(dòng)為當(dāng)前屏幕設(shè)置合適的contentsScale倍數(shù),自己手動(dòng)創(chuàng)建的layer需要明確的設(shè)置contentsScale, 否則它的倍數(shù)會(huì)為1拭荤,在高清屏上會(huì)失真茵臭。
可以設(shè)置很多參數(shù),字體類型舅世,字體大小,對(duì)齊方式奇徒,截?cái)嗟奈恢茫ㄩ_始雏亚,結(jié)束,早間)摩钙。github代碼