iOS 異步繪制與顯示的工具類:YYAsyncLayer
iOS 異步繪制與顯示的工具類。
(該工具是從 YYText 提取出來的獨立組件)
簡單用法
@interface YYLabel : UIView @property NSString *text; @property UIFont *font; @end @implementation YYLabel - (void)setText:(NSString *)text { _text = text.copy; [YYTransaction transactionWithTarget:self selector:@selector(contentsNeedUpdated)]; } - (void)setFont:(UIFont *)font { _font = font; [YYTransaction transactionWithTarget:self selector:@selector(contentsNeedUpdated)]; } - (void)layoutSubviews { [super layoutSubviews]; [YYTransaction transactionWithTarget:self selector:@selector(contentsNeedUpdated)]; } - (void)contentsNeedUpdated { // do update [self setNeedsDisplay]; } #pragma mark - YYAsyncLayer + (Class)layerClass { return YYAsyncLayer.class; } - (YYAsyncLayerDisplayTask *)newAsyncDisplayTask { // capture current state to display task NSString *text = _text; UIFont *font = _font; YYAsyncLayerDisplayTask *task = [YYAsyncLayerDisplayTask new]; task.willDisplay = ^(CALayer *layer) { //... }; task.display = ^(CGContextRef context, CGSize size, BOOL(^isCancelled)(void)) { if (isCancelled()) return; NSArray *lines = CreateCTLines(text, font, size.width); if (isCancelled()) return; for (int i = 0; i < lines.count; i++) { CTLineRef line = line[i]; CGContextSetTextPosition(context, 0, i * font.pointSize * 1.5); CTLineDraw(line, context); if (isCancelled()) return; } }; task.didDisplay = ^(CALayer *layer, BOOL finished) { if (finished) { // finished } else { // cancelled } }; return task; } @end
安裝
Cocoapods
- 在 Podfile 中添加pod "YYAsyncLayer"。
- 執行pod install或pod update。
- 導入 <YYAsyncLayer/YYAsyncLayer.h>。
Carthage
- 在 Cartfile 中添加github "ibireme/YYAsyncLayer"。
- 執行carthage update --platform ios并將生成的 framework 添加到你的工程。
- 導入 <YYAsyncLayer/YYAsyncLayer.h>。
手動安裝
- 下載 YYAsyncLayer 文件夾內的所有內容。
- 將 YYAsyncLayer 內的源文件添加(拖放)到你的工程。
- 導入YYAsyncLayer.h。
文檔
你可以在 CocoaDocs 查看在線 API 文檔,也可以用 appledoc 本地生成文檔。
系統要求
該項目最低支持 iOS 6.0。
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!