主要是使用UIBezierPath貝塞爾曲線設(shè)置陰影路徑曼验。
路徑還是比較規(guī)則的,就是直線加一個(gè)圓徽辰鬓照;而UIBezierPath對(duì)象畫直線和圓弧都是比較簡(jiǎn)單的。
稍微有一點(diǎn)拐彎的地方就是圓弧的起始角度和結(jié)束角度要計(jì)算下孤紧。
計(jì)算圓弧起始角度和結(jié)束角度
圖中結(jié)果很明顯
{\sin\alpha = a/r} ; \alpha = \arcsin(a/r)
很明顯起始角度:π+α 結(jié)束角度:2π - α
下面是完整代碼:
/**
給tabbar添加陰影豺裆,中間有一個(gè)圓形的凸起
@param center 圓弧中心
@param radius 圓弧半徑
*/
-(void)setTabBarShadowWithCenter:(CGPoint)center radius:(CGFloat)radius {
CAShapeLayer *layer = [CAShapeLayer new];
//背景填充色
layer.fillColor = [UIColor whiteColor].CGColor;
layer.shadowColor = [UIColor colorWithRGBHex:0x1D1B27 alpha:0.3].CGColor;
layer.shadowOffset = CGSizeMake(0, -4);
layer.shadowOpacity = 0.5;
CGFloat a = center.y;
CGFloat angle = asin(a/radius);
//初始化一個(gè)路徑
UIBezierPath* path = [UIBezierPath bezierPath];
//線條拐角
path.lineCapStyle = kCGLineCapRound;
//起點(diǎn)
[path moveToPoint:CGPointMake(0, 0)];
//繪制一條圓弧
[path addArcWithCenter:center radius:radius startAngle:angle + M_PI endAngle:2*M_PI - angle clockwise:YES];
[path addLineToPoint:CGPointMake(kScreenWidth, 0)];
[path addLineToPoint:CGPointMake(kScreenWidth, kSafeAreaBottomHeight)];
[path addLineToPoint:CGPointMake(0, kSafeAreaBottomHeight)];
// 根據(jù)坐標(biāo)點(diǎn)連線
// [path stroke];
layer.path = [path CGPath];
[path closePath];
layer.shadowPath = [path CGPath];
[self.layer insertSublayer:layer atIndex:0];
}