美文网首页
为UIButton添加单侧阴影效果

为UIButton添加单侧阴影效果

作者: 小手er冰凉 | 来源:发表于2017-06-06 10:31 被阅读53次

    前提很重要!!!必须要为UIButton添加背景颜色。没有背景颜色阴影不会出来的。

    另外还有一点,如果用Masonry做适配的话,添加圆角、边线等需要在前面加一句:

    [self.moreBtn layoutIfNeeded];

    这句话的主要作用就是加载刷新view,这样view才有frame值。

    言归正传,给button加单侧阴影主要是用UIBezierPath在需要加阴影的那一侧构建Path。

    [self.moreBtn layoutIfNeeded];

    self.moreBtn.layer.shadowColor = GrayColor.CGColor;//shadowColor阴影颜色

    self.moreBtn.layer.shadowOffset = CGSizeMake(0,0);//shadowOffset阴影偏移,x向右偏移-4,y向下偏移0,

    self.moreBtn.layer.shadowOpacity = 0.8;//阴影透明度,默认0

    self.moreBtn.layer.shadowRadius = 3;//阴影半径,默认3

    //路径阴影

    UIBezierPath *path = [UIBezierPath bezierPath];

    CGPoint leftMiddle = CGPointMake(0,0);

    [path moveToPoint:leftMiddle];

    [path addLineToPoint:CGPointMake(-2, 0)];

    [path addLineToPoint:CGPointMake(-2, self.moreBtn.height)];

    [path addLineToPoint:CGPointMake(0, self.moreBtn.height)];

    self.moreBtn.layer.shadowPath = path.CGPath;

    最终效果图:

    相关文章

      网友评论

          本文标题:为UIButton添加单侧阴影效果

          本文链接:https://www.haomeiwen.com/subject/gtyefxtx.html