當(dāng)我們開發(fā)app的一些新功能的時(shí)候,經(jīng)常會(huì)有需求,要求我們引導(dǎo)用戶去使用這些新功能,類似于下面的這種效果:
大家注意看,”知道啦“,”全新任務(wù)上線了“,這些字體,我們的UI們都使用了藝術(shù)字體。這是必須的。由于屏幕適配,藝術(shù)字體被拉伸了也看不出來(lái)呀~
所以,大家就可以想到我們這張引導(dǎo)頁(yè),其實(shí)整體使用的就是一張圖片。
敲重點(diǎn)?。???? ”任務(wù)中心“,這里的鏤空效果,就是我們接下來(lái)要詳細(xì)講解的部分。
兩種方案:
方案一:我們可以在灰色背景的上層加一層白色的背景,在白色背景上再添加圖片和文字,覆蓋掉底層的圖片和文字。
方案二:我們可以使用貝塞爾曲線來(lái)反向繪制一個(gè)鏤空的部分,這是我們重點(diǎn)講解的內(nèi)容:
???? //繪制一個(gè)全屏的透明背景View
? ? UIView *bgView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 375, 667)];
? ? [self.view addSubview:bgView];
? ? //貝塞爾曲線 畫一個(gè)矩形
? ? UIBezierPath *bpath = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 375, 667)];
? ? //bezierPathByReversingPath表示反向繪制
? ? [bpath appendPath:[[UIBezierPath bezierPathWithRect:CGRectMake(10, 200, 100, 100)] bezierPathByReversingPath]];
? ? //創(chuàng)建一個(gè)CAShapeLayer 圖層,黑色半透明背景
? ? CAShapeLayer *shapeLayer = [CAShapeLayer layer];
? ? shapeLayer.fillColor=[UIColor colorWithRed:0 green:0 blue:0 alpha:0.5].CGColor;
? ? shapeLayer.path = bpath.CGPath;
? ? [bgView.layer addSublayer:shapeLayer];
//最后添加圖片
? ? UIImageView *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 375, 667)];
? ? imageView.image=[UIImage imageNamed:@"image_1"];
? ? [bgView addSubview:imageView];