iOS设置某条边的阴影

无论需求多奇葩,一点一点实现它。

要设置某条边的阴影,还是使用shadowPath自己画出来才可以,画一个也是画,干脆就都画了,任意边的阴影都可以设置。作为一个扩展方法更好用

来先试一下看看效果

    UIView *testView = [[UIView alloc]init];
    testView.backgroundColor = UIColor.blueColor;
    testView.frame = CGRectMake(200, 200, 300, 300);
    testView.center = self.view.center;
    [self.view addSubview:testView];
    [testView addShdowColor:UIColor.redColor shadowOpacity:0.8 shadowRadius:4 shadowSide:BNShadowSideLeft|BNShadowSideRight];
Simulator Screen Shot - iPhone 11 - 2021-03-15 at 21.43.16.png

下面是代码实现

/// 使用位枚举指定圆角位置
/// 通过在各个边画矩形来实现shadowpath,真正实现指那儿打那儿
/// @param shadowColor 阴影颜色
/// @param shadowOpacity 阴影透明度
/// @param shadowRadius 阴影半径
/// @param shadowSide 阴影位置
-(void)addShdowColor:(UIColor *)shadowColor
       shadowOpacity:(CGFloat)shadowOpacity
        shadowRadius:(CGFloat)shadowRadius
          shadowSide:(BNShadowSide)shadowSide;

-(void)addShdowColor:(UIColor *)shadowColor shadowOpacity:(CGFloat)shadowOpacity shadowRadius:(CGFloat)shadowRadius shadowSide:(BNShadowSide)shadowSide{
    self.layer.masksToBounds = NO;
    self.layer.shadowColor = shadowColor.CGColor;
    self.layer.shadowRadius = shadowRadius;
    self.layer.shadowOpacity = shadowOpacity;
    
    // 默认值 0 -3
    // 使用默认值即可,这个各个边都要设置阴影的,自己调反而效果不是很好。
    //    self.layer.shadowOffset = CGSizeMake(0, -3);
    
    CGRect bounds = self.layer.bounds;
    CGFloat maxX = CGRectGetMaxX(bounds);
    CGFloat maxY = CGRectGetMaxY(bounds);
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    
    if (shadowSide & BNShadowSideTop) {
        // 上边
        [path moveToPoint:CGPointZero];
        [path addLineToPoint:CGPointMake(0, -shadowRadius)];
        [path addLineToPoint:CGPointMake(maxX, -shadowRadius)];
        [path addLineToPoint:CGPointMake(maxX, 0)];
    }
    
    if (shadowSide & BNShadowSideRight) {
        // 右边
        [path moveToPoint:CGPointMake(maxX, 0)];
        [path addLineToPoint:CGPointMake(maxX,maxY)];
        [path addLineToPoint:CGPointMake(maxX + shadowRadius, maxY)];
        [path addLineToPoint:CGPointMake(maxX + shadowRadius, 0)];
    }
    
    if (shadowSide & BNShadowSideBottom) {
        // 下边
        [path moveToPoint:CGPointMake(0, maxY)];
        [path addLineToPoint:CGPointMake(maxX,maxY)];
        [path addLineToPoint:CGPointMake(maxX, maxY + shadowRadius)];
        [path addLineToPoint:CGPointMake(0, maxY + shadowRadius)];
    }
    
    if (shadowSide & BNShadowSideLeft) {
        // 左边
        [path moveToPoint:CGPointMake(0, 0)];
        [path addLineToPoint:CGPointMake(-shadowRadius,0)];
        [path addLineToPoint:CGPointMake(-shadowRadius, maxY)];
        [path addLineToPoint:CGPointMake(0, maxY)];
    }
    
    self.layer.shadowPath = path.CGPath;
}

当然,用到的枚举需要在头文件中定义哦

typedef NS_OPTIONS(NSUInteger, BNShadowSide) {
    BNShadowSideNone   = 0,
    BNShadowSideTop    = 1 << 0,
    BNShadowSideLeft   = 1 << 1,
    BNShadowSideBottom = 1 << 2,
    BNShadowSideRight  = 1 << 3,
    BNShadowSideAll    = BNShadowSideTop | BNShadowSideLeft | BNShadowSideBottom | BNShadowSideRight
};

你可能感兴趣的:(iOS设置某条边的阴影)