如何添加一个下拉阴影的 UIButton?

我想添加一个下拉阴影的 UIButton。我尝试使用 self. layer.shadow * 属性。这些属性在 UIView 中工作,但在 UIButton 中的行为不同。如果我能得到任何指导来绘制阴影,我将非常感激。谢谢!

self.layer.cornerRadius = 8.0f;
self.layer.masksToBounds = YES;
self.layer.borderWidth = 1.0f;


self.layer.shadowColor = [UIColor greenColor].CGColor;
self.layer.shadowOpacity = 0.8;
self.layer.shadowRadius = 12;
self.layer.shadowOffset = CGSizeMake(12.0f, 12.0f);
90761 次浏览

您可以子类化 UIButton 并覆盖 draRect: 方法并手动添加阴影。这需要做更多的工作,现在您应该了解一些关于石英2d 的内容,但是结果正是您想要的。 否则,您可以只添加一个图像,但我更喜欢子类 UIButton,因为它对于按钮的大小非常灵活,它更一般。

问题中只有一个小错误导致阴影不显示: masksToBounds:YES也掩盖了阴影!这是正确的密码:

self.layer.cornerRadius = 8.0f;
self.layer.masksToBounds = NO;
self.layer.borderWidth = 1.0f;


self.layer.shadowColor = [UIColor greenColor].CGColor;
self.layer.shadowOpacity = 0.8;
self.layer.shadowRadius = 12;
self.layer.shadowOffset = CGSizeMake(12.0f, 12.0f);

不幸的是,这意味着我们不能掩盖的内容,并有一个阴影在同一时间没有技巧。

记住 #import <QuartzCore/QuartzCore.h>

这里有一个子类,不仅创建下拉阴影,但按钮动画下来,当你按下它。

//
//  ShadowButton.h


#import <Foundation/Foundation.h>


@interface ShadowButton : UIButton {
}


@end


//
//  ShadowButton.m


#import "ShadowButton.h"
#import <QuartzCore/QuartzCore.h>


@implementation ShadowButton


-(void)setupView{


self.layer.shadowColor = [UIColor blackColor].CGColor;
self.layer.shadowOpacity = 0.5;
self.layer.shadowRadius = 1;
self.layer.shadowOffset = CGSizeMake(2.0f, 2.0f);


}


-(id)initWithFrame:(CGRect)frame{
if((self = [super initWithFrame:frame])){
[self setupView];
}


return self;
}


-(id)initWithCoder:(NSCoder *)aDecoder{
if((self = [super initWithCoder:aDecoder])){
[self setupView];
}


return self;
}


-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
self.contentEdgeInsets = UIEdgeInsetsMake(1.0,1.0,-1.0,-1.0);
self.layer.shadowOffset = CGSizeMake(1.0f, 1.0f);
self.layer.shadowOpacity = 0.8;


[super touchesBegan:touches withEvent:event];


}


-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
self.contentEdgeInsets = UIEdgeInsetsMake(0.0,0.0,0.0,0.0);
self.layer.shadowOffset = CGSizeMake(2.0f, 2.0f);
self.layer.shadowOpacity = 0.5;


[super touchesEnded:touches withEvent:event];


}


@end

如果你使用 UIButton,这里有一个简单的解决方案:

#import <QuartzCore/QuartzCore.h>


button.imageView.layer.cornerRadius = 7.0f;
button.layer.shadowRadius = 3.0f;
button.layer.shadowColor = [UIColor blackColor].CGColor;
button.layer.shadowOffset = CGSizeMake(0.0f, 1.0f);
button.layer.shadowOpacity = 0.5f;
button.layer.masksToBounds = NO;

刚刚修好,觉得值得发布,希望能有帮助!

你可以在 Xcode 创建一个子类并配置它的值

下面是一个例子:

import UIKit
import QuartzCore


@IBDesignable
class CustomButton: UIButton {


@IBInspectable var cornerRadius: CGFloat = 0 {
didSet {
layer.cornerRadius = cornerRadius
}
}


@IBInspectable var borderWidth: CGFloat = 0 {
didSet {
layer.borderWidth = borderWidth
}
}


@IBInspectable var borderColor: UIColor = UIColor.gray {
didSet {
layer.borderColor = borderColor.cgColor
}
}


@IBInspectable var shadowColor: UIColor = UIColor.gray {
didSet {
layer.shadowColor = shadowColor.cgColor
}
}


@IBInspectable var shadowOpacity: Float = 1.0 {
didSet {
layer.shadowOpacity = shadowOpacity
}
}


@IBInspectable var shadowRadius: CGFloat = 1.0 {
didSet {
layer.shadowRadius = shadowRadius
}
}


@IBInspectable var masksToBounds: Bool = true {
didSet {
layer.masksToBounds = masksToBounds
}
}


@IBInspectable var shadowOffset: CGSize = CGSize(width: 12, height: 12) {
didSet {
layer.shadowOffset = shadowOffset
}
}




}