简体   繁体   English

如何在iOS UIButton中添加投影?

[英]How to add Drop Shadow in iOS UIButton?

Help of experts is needed 需要专家的帮助

I am making UIButton custom, where I am cropping of background and showing on UIButton as below. 我正在自定义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;

}

Now I want to apply, the property of Drop Shadow, seen in Adobe Photoshop to this Image, 现在,我想将在Adobe Photoshop中看到的“投影”的属性应用于此图像,

How can I do so using Core Graphics or any Core Foundation of iOS Library? 如何使用Core Graphics或iOS库的任何Core Foundation这样做?

Drop Shadow of Adobe looks as below Adobe的阴影如下所示

在此处输入图片说明 Thanks 谢谢

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM