如何在iOS UIButton中添加Drop Shadow?

时间:2015-09-01 11:09:24

标签: ios uibutton dropshadow

需要专家帮助

我正在制作UIButton自定义,我正在裁剪背景并在UIButton上显示如下。

[self setBackgroundImage:[self getSingleColorImageForLinear:self.frame] forState:UIControlStateSelected];




 //Method to make take image
-(UIImage *)getSingleColorImageForLinear:(CGRect)frame{


    CGSize size = CGSizeMake(frame.size.width,frame.size.height);
    UIGraphicsBeginImageContextWithOptions(size, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();

    size_t gradientNumberOfLocations = 1;
    CGFloat gradientLocations[1] = { 0.0 };
    CGFloat gradientComponents[4] = { 0, 0, 0, 0.3, };

    CGGradientRef gradient = CGGradientCreateWithColorComponents (colorspace, gradientComponents, gradientLocations, gradientNumberOfLocations);

    CGContextDrawLinearGradient(context, gradient, CGPointMake(0, 0), CGPointMake(0, size.height), 0);


    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    CGGradientRelease(gradient);
    CGColorSpaceRelease(colorspace);
    UIGraphicsEndImageContext();

    return image;

}

现在我想申请Drop Shadow的属性,在Adobe Photoshop中看到此图片,

如何使用Core Graphics或任何iOS库的Core Foundation?

Adob​​e的阴影看起来如下

enter image description here 感谢

1 个答案:

答案 0 :(得分:4)

button.layer.shadowColor = [UIColor blackColor].CGColor;
button.layer.shadowOffset = CGSizeMake(5, 5);
button.layer.shadowRadius = 5;
button.layer.shadowOpacity = 0.5;