国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > iOS > 正文

講解IOS 中CALayer繪制圖片的代碼示例

2020-02-19 15:46:29
字體:
供稿:網(wǎng)友

在iOS中,我們可以在任何地方看到華麗的動畫效果,隨著互聯(lián)網(wǎng)的發(fā)展,我們要實現(xiàn)麗的動畫效果其實是很簡單的,今天武林技術(shù)頻道小編就帶大家講解IOS 中CALayer繪制圖片的代碼示例吧!

IOS 中CALayer繪制圖片的實例詳解

CALayer渲染內(nèi)容圖層。與UIImageView相比,不具有事件響應(yīng)功能,且UIImageView是管理內(nèi)容。

注意事項:如何使用delegate對象執(zhí)行代理方法進行繪制,切記需要將delegate設(shè)置為nil,否則會導致異常crash。

CALayer繪制圖片與線條效果圖:

代碼示例:

CGPoint position = CGPointMake(160.0, 200.0); CGRect bounds = CGRectMake(0.0, 0.0, 150.0, 150.0); CGFloat cornerRadius = 150.0 / 2; CGFloat borderWidth = 2.0; 
// 陰影層 CALayer *layerShadow = [[CALayer alloc] init]; layerShadow.position = position; layerShadow.bounds = bounds; layerShadow.cornerRadius = cornerRadius; layerShadow.borderWidth = borderWidth; layerShadow.borderColor = [UIColor whiteColor].CGColor; layerShadow.shadowColor = [UIColor grayColor].CGColor; layerShadow.shadowOffset = CGSizeMake(2.0, 1.0); layerShadow.shadowOpacity = 1.0; layerShadow.shadowRadius = 3.0; [self.view.layer addSublayer:layerShadow]; 
// 容器層 CALayer *layerContant = [[CALayer alloc] init]; // 添加到父圖層 [self.view.layer addSublayer:layerContant]; // 圖層中心點、大小(中心點和大小構(gòu)成frame) layerContant.position = position; layerContant.bounds = bounds; // 圖層背景顏色 layerContant.backgroundColor = [UIColor redColor].CGColor; // 圖層圓角半徑 layerContant.cornerRadius = cornerRadius; // 圖層蒙版、子圖層是否剪切圖層邊界 //  layerContant.mask = nil; layerContant.masksToBounds = YES; // 邊框?qū)挾取㈩伾?layerContant.borderWidth = borderWidth; layerContant.borderColor = [UIColor whiteColor].CGColor; // 陰影顏色、偏移量、透明度、形狀、模糊半徑 //  layerContant.shadowColor = [UIColor grayColor].CGColor; //  layerContant.shadowOffset = CGSizeMake(2.0, 1.0); //  layerContant.shadowOpacity = 1.0; //  CGMutablePathRef path = CGPathCreateMutable();   //  layerContant.shadowPath = path; //  layerContant.shadowRadius = 3.0; // 圖層透明度 layerContant.opacity = 1.0; 
// 繪制圖片顯示方法1 // 圖層形變 // 旋轉(zhuǎn)(angle轉(zhuǎn)換弧度:弧度=角度*M_PI/180;x上下對換、y左右對換、z先上下對換再左右對換;-1.0~1.0) //  layerContant.transform = CATransform3DMakeRotation(M_PI, 0.0, 0.0, 0.0); // 縮放(0.0~1.0) //  layerContant.transform = CATransform3DMakeScale(0.8, 0.8, 0.8); // 移動 //  layerContant.transform = CATransform3DMakeTranslation(10.0, 1.0, 1.0); // 顯示內(nèi)容  [layerContant setContents:[UIImage imageNamed:@"header"].CGImage]; 

?繪制圖片顯示方法2?

layerContant.delegate = self; [layerContant setNeedsDisplay];  - (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {   // 繪圖   CGContextSaveGState(ctx);   // 圖形上下文形變,避免圖片倒立顯示   CGContextScaleCTM(ctx, 1.0, -1.0);   CGContextTranslateCTM(ctx, 0.0, -150.0);   // 圖片   UIImage *image = [UIImage imageNamed:@"header"];   CGContextDrawImage(ctx, CGRectMake(0.0, 0.0, 150.0, 150.0), image.CGImage);   CGContextRestoreGState(cox); } 
// 繪制實線、虛線 - (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {     // 繪實線   // 線條寬   CGContextSetLineWidth(ctx, 1.0);   // 線條顏色 //  CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0);   CGContextSetStrokeColorWithColor(ctx, [UIColor greenColor].CGColor);   // 方法1   // 坐標點數(shù)組   CGPoint aPoints[2];   aPoints[0] = CGPointMake(10.0, 50.0);   aPoints[1] = CGPointMake(140.0, 50.0);   // 添加線 points[]坐標數(shù)組,和count大小   CGContextAddLines(ctx, aPoints, 2);   // 根據(jù)坐標繪制路徑   CGContextDrawPath(ctx, kCGPathStroke);   // 方法2   CGContextSetLineWidth(ctx, 5.0);   CGContextSetStrokeColorWithColor(ctx, [UIColor purpleColor].CGColor);   CGContextMoveToPoint(ctx, 10.0, 60.0); // 起點坐標   CGContextAddLineToPoint(ctx, 140.0, 60.0); // 終點坐標   CGContextStrokePath(ctx); // 繪制路徑      // 繪虛線   // 線條寬   CGContextSetLineWidth(ctx, 2.0);   // 線條顏色   CGContextSetStrokeColorWithColor(ctx, [UIColor blueColor].CGColor);   // 虛線   CGFloat dashArray[] = {1, 1, 1, 1};   CGContextSetLineDash(ctx, 1, dashArray, 1);   // 起點   CGContextMoveToPoint(ctx, 10.0, 100.0);   // 終點   CGContextAddLineToPoint(ctx, 140.0, 100.0);   // 繪制路徑   CGContextStrokePath(ctx); } 
// 內(nèi)存管理,避免異常crash - (void)dealloc {   for (CALayer *layer in self.view.layer.sublayers)   {     if ([layer.delegate isEqual:self])     {       layer.delegate = nil;     }   }   NSLog(@"%@ 被釋放了~", self); } 上文就是講解IOS 中CALayer繪制圖片的代碼示例,希望了解以后對你有幫助哦,網(wǎng)絡(luò)的普及給大家?guī)砹撕芏鄬W習機會,就看你學習的意愿夠不夠強烈了。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 靖远县| 郸城县| 舒城县| 来凤县| 凤凰县| 平山县| 临沭县| 清镇市| 延边| 体育| 鹤庆县| 疏勒县| 河北区| 武威市| 稻城县| 武鸣县| 鲁山县| 永年县| 九龙县| 洛南县| 津市市| 曲阳县| 梅州市| 西城区| 安图县| 大石桥市| 察雅县| 山丹县| 汝州市| 泊头市| 秦皇岛市| 绥德县| 江门市| 饶河县| 葵青区| 东明县| 怀化市| 建始县| 长垣县| 家居| 合作市|