内部阴影到UIImageView仅限顶部和底部(目标C)

时间:2018-03-20 14:23:01

标签: ios objective-c cagradientlayer

  

我想将内部阴影添加到我的UIImageView中,如下所示:(以下是需要在图像视图上添加的预期阴影)

enter image description here

  

在顶部和底部应用的阴影框架不在前导和底部   尾随。

     

我已尝试过以下代码但未获得预期的输出。

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    UIView * mapCover = [UIView new];
    CGRect frameImage = self.bgImageView.frame;
    frameImage.origin.x = -50;
    frameImage.size.width = frameImage.size.width+50;
    mapCover.frame = frameImage;
    [self.view addSubview:mapCover];

    CAGradientLayer * vertical = [CAGradientLayer layer];
    vertical.frame = mapCover.bounds;
    vertical.colors = [NSArray arrayWithObjects:(id)[UIColor blackColor].CGColor,
                       (id)[[UIColor whiteColor] colorWithAlphaComponent:0.0f].CGColor,
                       (id)[[UIColor whiteColor] colorWithAlphaComponent:0.0f].CGColor,
                       (id)[UIColor whiteColor].CGColor, nil];
    vertical.locations = @[@0.01,@0.1,@0.9,@0.99];
    [mapCover.layer insertSublayer:vertical atIndex:0];

    CAGradientLayer * horizontal = [CAGradientLayer layer];
    horizontal.frame = mapCover.bounds;
    horizontal.colors = [NSArray arrayWithObjects:(id)[UIColor whiteColor].CGColor,
                         (id)[[UIColor whiteColor] colorWithAlphaComponent:0.0f].CGColor,
                         (id)[[UIColor whiteColor] colorWithAlphaComponent:0.0f].CGColor,
                         (id)[UIColor whiteColor].CGColor, nil];
    horizontal.locations = @[@0.01,@0.1,@0.9,@0.99];
    horizontal.startPoint = CGPointMake(0.0, 0.5);
    horizontal.endPoint = CGPointMake(1.0, 0.5);
    [self.bgImageView.layer insertSublayer:horizontal atIndex:0];
}

1 个答案:

答案 0 :(得分:0)

  

<强>解决!!!

     

使用下面链接中的UIView类别:它具有枚举   首页|底部|左|右

// Add a shadow to the top and bottom of the view
[exampleView addInnerShadowWithRadius:3.0f
                             andColor:[UIColor colorWithWhite:0 alpha:0.45f]
                          inDirection:NLInnerShadowDirectionBottom | NLInnerShadowDirectionTop];

http://www.nathanlampi.com/posts/10002/UIView-Inner-Shadow.html

enter image description here