萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> iOS 開發

iOS 代碼實現 UIView 陰影效果:以自定義 UIButton 為例

為控件添加陰影能顯著提升視覺層次。通過繼承 UIButton 重寫 drawRect: 並配合 Core Graphics,可實現高性能、可復用的陰影按鈕。

自定義 ShadowButton 完整實現

ShadowButton.h

@interface ShadowButton : UIButton

@property (nonatomic, strong) UIColor *shadowColor;
@property (nonatomic, assign) CGSize shadowOffset;
@property (nonatomic, assign) CGFloat shadowRadius;
@property (nonatomic, assign) float shadowOpacity;

@end

ShadowButton.m

@implementation ShadowButton

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupDefaults];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)coder {
    self = [super initWithCoder:coder];
    if (self) {
        [self setupDefaults];
    }
    return self;
}

- (void)setupDefaults {
    self.shadowColor = [UIColor grayColor];
    self.shadowOffset = CGSizeMake(2, 3);
    self.shadowRadius = 4;
    self.shadowOpacity = 0.5;
    // 關鍵:layer 默認不裁剪陰影,需設置 masksToBounds = NO
    self.layer.masksToBounds = NO;
}

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect]; // 繪制默認內容(圖片、標題)
    
    // 可選:在這裡用 Core Graphics 繪制自定義陰影路徑
    // 但通常直接配置 layer.shadowPath 更高效
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    // 設置 layer 陰影屬性(比 drawRect 繪制更高效、支持動畫)
    self.layer.shadowColor = self.shadowColor.CGColor;
    self.layer.shadowOffset = self.shadowOffset;
    self.layer.shadowRadius = self.shadowRadius;
    self.layer.shadowOpacity = self.shadowOpacity;
    
    // 優化:指定 shadowPath 避免離屏渲染
    self.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.bounds].CGPath;
    self.layer.shouldRasterize = YES;
    self.layer.rasterizationScale = [UIScreen mainScreen].scale;
}

@end

使用示例

ShadowButton *btn = [[ShadowButton alloc] initWithFrame:CGRectMake(50, 100, 200, 50)];
[btn setTitle:@"帶陰影按鈕" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
btn.backgroundColor = [UIColor systemBlueColor];
btn.shadowColor = [UIColor blackColor];
btn.shadowOffset = CGSizeMake(0, 4);
btn.shadowRadius = 8;
btn.shadowOpacity = 0.3;
[self.view addSubview:btn];

Core Graphics 手繪陰影(特殊形狀需求)

若按鈕為非矩形(如圓角、圖標),需在 drawRect: 中繪制:

- (void)drawRect:(CGRect)rect {
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    // 陰影設置
    CGContextSetShadowWithColor(ctx, self.shadowOffset, self.shadowRadius, self.shadowColor.CGColor);
    
    // 繪制圓角矩形路徑
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.bounds cornerRadius:8];
    [self.backgroundColor setFill];
    [path fill];
    
    // 繪制標題/圖片(需手動計算位置)
    // ...
}

性能與最佳實踐

  • 優先用 Layer 陰影:GPU 加速、可動畫、無需重寫 drawRect。
  • 必須設置 shadowPath:避免離屏渲染,大幅提升滾動流暢度。
  • shouldRasterize = YES:將陰影光柵化緩存,適合靜態陰影。
  • 避免 masksToBounds = YES:會裁剪掉陰影,互斥關係。
  • 復用 Cell 中:在 prepareForReuse 重置陰影屬性,防止異常殘留。

Swift 版本片段

class ShadowButton: UIButton {
    @IBInspectable var shadowColor: UIColor = .gray
    @IBInspectable var shadowOffset: CGSize = .init(width: 2, height: 3)
    @IBInspectable var shadowRadius: CGFloat = 4
    @IBInspectable var shadowOpacity: Float = 0.5
    
    override func layoutSubviews() {
        super.layoutSubviews()
        layer.shadowColor = shadowColor.cgColor
        layer.shadowOffset = shadowOffset
        layer.shadowRadius = shadowRadius
        layer.shadowOpacity = shadowOpacity
        layer.shadowPath = UIBezierPath(rect: bounds).cgPath
        layer.shouldRasterize = true
        layer.rasterizationScale = UIScreen.main.scale
        layer.masksToBounds = false
    }
}

封裝通用陰影組件,讓設計還原不再依賴切圖。

copyright © 萬盛學電腦網 all rights reserved