關(guān)于利用CAShapeLayer設(shè)置遮罩和圖形,貝塞爾曲線的傻瓜運(yùn)用

在工程中有一些需求需要設(shè)置部分遮罩,如二維碼掃描區(qū)域的圖形繪畫,類似于頭像圓角等,這時(shí)候變需要CAShapeLayer,UIBezierPath等進(jìn)行繪圖
簡(jiǎn)單的代碼如下
Simulator Screen Shot - iPhone 6s - 2018-02-24 at 14.25.31.png
//設(shè)置背景色為白色
self.view.backgroundColor = [UIColor whiteColor];
//繪制200*200的方框
[self setCropRect:CGRectMake((self.view.bounds.size.width-200)/2,
                                 (self.view.bounds.size.height-200)/2, 200, 200)];

- (void)setCropRect:(CGRect)cropRect{
    _testShaplayer= [[CAShapeLayer alloc] init];
    //繪制兩條path
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathAddRect(path, nil, cropRect);
    CGPathAddRect(path, nil, self.view.bounds);
    //填充兩條path的非交集
    [_testShaplayer setFillRule:kCAFillRuleEvenOdd];
    [_testShaplayer setPath:path];
    //顏色是黑色透明度0.6
    [_testShaplayer setFillColor:[UIColor blackColor].CGColor];
    [_testShaplayer setOpacity:0.6];
    
    
    [_testShaplayer setNeedsDisplay];
    
    [self.view.layer addSublayer:_testShaplayer];
    
}
Simulator Screen Shot - iPhone 6s - 2018-02-24 at 14.25.05.png
- (void)configRoundShape {
    self.view.backgroundColor = [UIColor whiteColor];
    _testShaplayer = [[CAShapeLayer alloc]init];
    //繪制貝塞爾曲線
    UIBezierPath *roundPath = [UIBezierPath bezierPathWithArcCenter:self.view.center radius:self.view.bounds.size.width/2 startAngle:0.0 endAngle:M_PI*2 clockwise:YES];
    UIBezierPath *rectPath = [UIBezierPath bezierPathWithRect:self.view.bounds];
    [roundPath appendPath:rectPath];
    //同樣取非交集繪制
    [_testShaplayer setFillRule:kCAFillRuleEvenOdd];
    [_testShaplayer setPath:roundPath.CGPath];
    [_testShaplayer setFillColor:[UIColor blackColor].CGColor];
    [_testShaplayer setOpacity:0.6];
    
    [_testShaplayer setNeedsDisplay];
    
    [self.view.layer addSublayer:_testShaplayer];
}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 目錄: 主要繪圖框架介紹 CALayer 繪圖 貝塞爾曲線-UIBezierPath CALayer子類 補(bǔ)充:i...
    Ryan___閱讀 1,777評(píng)論 1 9
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,256評(píng)論 4 61
  • 假性親密關(guān)系——你以為很近,其實(shí)你們很遠(yuǎn) 原文:微信-在假性親密關(guān)系中逃避真的親密感? 真正的親密關(guān)系是你與另一個(gè)...
    relyingon閱讀 1,523評(píng)論 0 4
  • 隨著人們物質(zhì)文化生活的提高,大家慢慢的開始追求精神上面的滿足,可仍然還有一部分人對(duì)于“吃”——念念不忘,下面就...
    獨(dú)自上路閱讀 335評(píng)論 3 2
  • 這兩天打開空間和朋友圈,基本都是關(guān)于舊歲的結(jié)語(yǔ)和新年展望,一片光明。 向來(lái)是喜歡湊熱鬧但熱鬧過后特別清靜甚至覺得壓...
    簡(jiǎn)萌呆呆閱讀 187評(píng)論 0 0

友情鏈接更多精彩內(nèi)容