IOS--CALayer的介绍及使用技巧
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
? 首先對CALayer進(jìn)行簡單的介紹:
1.在UIView中,CALayer只是一個(gè)類的聲明,因此需要添加?QuartzCore框架
2.UIKit框架只能應(yīng)用到ios中,但是Quartz2D是跨平臺的,因此在使用顏色時(shí)應(yīng)該將UIColor轉(zhuǎn)換成CGColor
3.修改圖層相當(dāng)于修改了UIView屬性,即修改了界面屬性
4.形變屬性既可以使用形變函數(shù)制定,也可以使用keypath制定
創(chuàng)建imageView并設(shè)置邊框?qū)傩?#xff08;基礎(chǔ))
1.bounds:寬度和高度,x y設(shè)置為0;
2.position:位置(默認(rèn)指中心點(diǎn),具體由anchorPoint決定)
3.anchorPoint:錨點(diǎn),(x,y的范圍都是0~1),決定了position的含義
4.backgroundColor:背景顏色(CGColorRef 類型)
5.borderColor:邊框顏色
6.borderWidth:邊框?qū)挾?/span>
7.borderRadius:圓角半徑
8.contents:內(nèi)容
???UIImageView?*imgView?=?[[UIImageView?alloc]?initWithFrame:CGRectMake(90,?90,?90,?90)];imgView.backgroundColor?=?[UIColor?redColor];imgView.image?=?[UIImage?imageNamed:@"1.jpg"];[self.view?addSubview:imgView];//1.獲取layer設(shè)置邊框imgView.layer.borderWidth?=?1;imgView.layer.borderColor?=?[UIColor?darkGrayColor].CGColor;//?2.設(shè)置弧度imgView.layer.cornerRadius?=?45;imgView.layer.masksToBounds?=?YES;imgView.clipsToBounds?=?YES;//3.設(shè)置陰影//如果設(shè)置imgView.layer.masksToBounds?=?YES;??則不會出現(xiàn)陰影imgView.layer.shadowColor?=?[UIColor?redColor].CGColor;imgView.layer.shadowOffset?=?CGSizeMake(5,?5);imgView.layer.shadowOpacity?=?0.5;//對imgView的layer層的transform?屬性進(jìn)行操作(第一種方法)//平移imgView.layer.transform?=?CATransform3DMakeTranslation(0,?200,?0);//旋轉(zhuǎn)imgView.layer.transform?=?CATransform3DMakeRotation(M_PI_4,?1,?0,?0);//縮放imgView.layer.transform?=?CATransform3DMakeScale(2,?2,?1);//對imgView的layer層的transform?屬性進(jìn)行操作(第二種方法)通過kvc的方法//平移[imgView.layer?setValue:@100?forKeyPath:@"transform.translation.x"];//旋轉(zhuǎn)[imgView.layer?setValue:@M_PI_2?forKeyPath:@"transform.rotation.z"];//縮放[imgView.layer?setValue:@0.5?forKeyPath:@"transform.scale.x"];? CALayer的基本屬性
? CALayer中的隱式動(dòng)畫(隱式動(dòng)畫的默認(rèn)時(shí)間為0.25秒)
? ?每一個(gè)UIView內(nèi)部都默認(rèn)關(guān)聯(lián)著一個(gè)CALayer,稱這個(gè)layer為Root Layer;
所有的非Root Layer都存在著隱式動(dòng)畫,默認(rèn)時(shí)長為1/4秒
bounds:縮放動(dòng)畫
position:平移動(dòng)畫
opacity:淡入淡出的動(dòng)畫(改變透明度)
更多的隱式動(dòng)畫屬性可在文檔中搜索
使用CALayer繪圖,會調(diào)用自己的drawRect方法
//點(diǎn)擊界面觸觸發(fā)的方法 -?(void)touchesBegan:(NSSet?*)touches?withEvent:(UIEvent?*)event?{//隱式動(dòng)畫的時(shí)間:0.25秒//如果不想存在動(dòng)畫//關(guān)閉動(dòng)畫 //????[CATransaction?begin]; //????[CATransaction?setDisableActions:YES];//取得點(diǎn)擊的坐標(biāo)UITouch?*touch?=?[touches?anyObject];CGPoint?point?=?[touch?locationInView:self.view];//1.修改layer的位置_layer.position?=?point;//2、改變layer的大小CGFloat?num?=?arc4random_uniform(50)?+?30;_layer.bounds?=?CGRectMake(0,?0,?num,?num);//3、修改layer的透明度_layer.opacity?=?arc4random_uniform(10)*0.1;//4、背景顏色_layer.backgroundColor?=?[UIColor?colorWithRed:arc4random_uniform(10)*0.1?green:arc4random_uniform(10)*0.1?blue:arc4random_uniform(10)*0.1?alpha:1].CGColor;//5、設(shè)置邊框的弧度_layer.cornerRadius?=?arc4random_uniform(20);//6、transform_layer.transform?=?CATransform3DMakeScale(2,?1,?1);//提交[CATransaction?commit];}轉(zhuǎn)載于:https://my.oschina.net/zhangqy/blog/507833
總結(jié)
以上是生活随笔為你收集整理的IOS--CALayer的介绍及使用技巧的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: IIS7配置PHP 报错 对找不到的文件
- 下一篇: Java中使用OpenSSL生成的RSA