如何将阴影添加到UIButton?
我想给UIButton添加阴影。我试图使用self.layer.shadow *属性。这些属性在UIView中工作,但它们在UIButton中表现不同。我真的很感激它,如果我能得到任何指针来绘制阴影。谢谢!如何将阴影添加到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);
您可以继承UIButton并覆盖drawRect:方法并手动添加阴影。这是更多的工作,你现在应该关于石英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
简单实用,谢谢 – webo80 2015-12-15 11:51:03
这里有一个简单的解决方案,如果您使用的是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.grayColor() {
didSet {
layer.borderColor = borderColor.CGColor
}
}
@IBInspectable var shadowColor: UIColor = UIColor.grayColor() {
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 = CGSizeMake(12.0, 12.0) {
didSet {
layer.shadowOffset = shadowOffset
}
}
}
核心动画指南,http://developer.apple.com/mac/library/documentation /cocoa/conceptual/CoreAnimation_guide/Articles/LayerVisProps.html说:iPhone OS注意:作为性能的考虑因素,iPhone OS不支持shadowColor,shadowOffset,shadowOpacity和shadowRadius属性。荡。 – 2010-04-27 20:13:22
自iOS 3.2以来,现在支持此属性。 Regards, – Quentin 2010-08-11 08:53:27