【问题标题】:How to add drop shadow only for the right side?如何仅为右侧添加阴影?
【发布时间】:2017-01-01 11:52:09
【问题描述】:

我正在添加这样的阴影。

vwVertical=[[UIView alloc] init];
[vwVertical setBackgroundColor:[UIColor whiteColor]];
vwVertical.translatesAutoresizingMaskIntoConstraints = NO;
vwVertical.layer.shadowColor=[UIColor colorWithRed:32/255 green:59/255 blue:90/255 alpha:1.0].CGColor;
vwVertical.layer.shadowOffset=CGSizeMake(5, 0);
vwVertical.layer.shadowOpacity=0.12;
vwVertical.layer.shadowRadius=6.5;
[vwBlock addSubview:vwVertical];

但这是添加阴影 3 面。我怎样才能添加到右侧。 请帮我。 谢谢

【问题讨论】:

    标签: ios uiview dropshadow


    【解决方案1】:

    您需要做的就是在top - left - bottom 上插入视图的边界,然后使用shadow path

    vwVertical=[[UIView alloc] init];
    // Create edge insets
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    // Create rect with inset and view's bounds
    CGRect shadowPathOnlyIncludingRight = UIEdgeInsetsInsetRect(vwVertical.bounds, contentInsets);
    // Apply it on the layer's shadowPath property
    vwVertical.layer.shadowPath = [UIBezierPath bezierPathWithRect:shadowPathOnlyIncludingRight].CGPath;
    
    [vwVertical setBackgroundColor:[UIColor whiteColor]];
    vwVertical.translatesAutoresizingMaskIntoConstraints = NO;
    vwVertical.layer.shadowColor=[UIColor colorWithRed:32/255 green:59/255 blue:90/255 alpha:1.0].CGColor;
    vwVertical.layer.shadowOffset=CGSizeMake(5, 0);
    vwVertical.layer.shadowOpacity=0.12;
    vwVertical.layer.shadowRadius=6.5;
    

    【讨论】:

    • 但右侧缺少顶部和底部 10 个像素的阴影..
    • @AlexandreG 我已经更新了我的答案。奇怪的是,当我最初回答时,我并没有遇到你刚刚报告的问题。
    • 但是现在阴影不仅仅出现在右侧。我已经复制粘贴了上面的代码,只是让阴影不透明度更高,所以对眼睛更明显:goloskok.com/u/image-6UQ1XEYirw.png
    • @AlexandreG 这就是最初的问题。如何仅在右侧投影。
    • 没错。在屏幕截图中(以及您的代码中),它显然不仅适用于右侧
    猜你喜欢
    • 2021-03-18
    • 1970-01-01
    • 2012-08-13
    • 2021-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-22
    相关资源
    最近更新 更多