平時開發(fā)中經(jīng)常被要求使用一個帶拱形的tabBar,效果如下圖所示
效果圖.PNG
系統(tǒng)只帶的tabBar是一個固定高度的矩形View, 所以我們只能自定義一個UIView。
由于需要畫一個拱形,拱形即為一個半圓,所以首先想到的是使用貝塞爾曲線。如果沒有接觸過貝塞爾曲線可以先百度下相關(guān)知識,可能看的有點(diǎn)暈哈,都是數(shù)學(xué)介紹。但是不要擔(dān)心,蘋果已經(jīng)為我們封裝好了,我們只需要調(diào)用下API就可實現(xiàn)了。
既然是自定義一個UIView,我們還是從- (void)drawRect:(CGRect)rect這個函數(shù)開始,好了廢話不多說直接上代碼
- (void)drawRect:(CGRect)rect
{
CGSize size = self.bounds.size;
CGFloat w = size.width/NUM;
CGFloat radius = w / 2;
CGFloat h = size.height - 49;
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(0, h)];
[path addLineToPoint:CGPointMake(2*w, h)];
[ path addArcWithCenter:CGPointMake(2*w + w/2 ,h) radius:radius startAngle:M_PI endAngle:0 clockwise:YES];
[path addLineToPoint:CGPointMake(size.width, h)];
[path addLineToPoint:CGPointMake(size.width, size.height)];
[path addLineToPoint:CGPointMake(0, size.height)];
[path addLineToPoint:CGPointMake(0, h)];
[path closePath];
CAShapeLayer *layer = [CAShapeLayer layer];
layer.path = path.CGPath;
self.layer.mask = layer;
}
給我們自定義的UIView設(shè)置個Frame值
HTTringleView *VC = [[HTTringleView alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height-49 - [UIScreen mainScreen].bounds.size.width/5, [UIScreen mainScreen].bounds.size.width, 49 + [UIScreen mainScreen].bounds.size.width/5)];
[self.view addSubview:VC];
就可以實現(xiàn)一個帶拱形的tabBar了,就是這么簡單,這么任性!

帶拱形的Tabbar.png
是不是很簡單,關(guān)鍵就是找好6個關(guān)鍵點(diǎn)CGPointMake(0, h), CGPointMake(2w, h),CGPointMake(2w + w/2 ,h),CGPointMake(size.width, h),CGPointMake(size.width, size.height),CGPointMake(0, size.height)。

6個關(guān)鍵點(diǎn).png
不斷的利用UIBezierPath在這6個關(guān)鍵點(diǎn)之間畫線(addLineToPoint)和畫圓(addArcWithCenter)。
最后我把代碼上傳GitHub給有需要的同學(xué)參考下。
思考:
如何畫一個帶尖角的UIView?

帶尖角的UIView.png