2010-02-23 40 views
63

Tôi muốn thêm bóng đổ vào UIButton. Tôi đã cố sử dụng các thuộc tính self.layer.shadow *. Những thuộc tính này hoạt động trong UIView, nhưng chúng hoạt động khác nhau trong UIButton. Tôi thực sự sẽ đánh giá cao nếu tôi có thể nhận được bất kỳ con trỏ nào để vẽ bóng đổ. Cảm ơn bạn!Làm cách nào để thêm bóng đổ vào 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); 
+0

Core Animation Hướng dẫn, http://developer.apple.com/mac/library/documentation /cocoa/conceptual/CoreAnimation_guide/Articles/LayerVisProps.html, nói: Hệ điều hành iPhone Lưu ý: Khi xem xét hiệu suất, hệ điều hành iPhone không hỗ trợ các thuộc tính shadowColor, shadowOffset, shadowOpacity và shadowRadius. Dang. –

+0

Thuộc tính này hiện được hỗ trợ kể từ iOS 3.2. Trân trọng, – Quentin

Trả lời

2

Bạn có thể phân lớp UIButton và ghi đè phương thức drawRect: và thêm bóng thả theo cách thủ công. Đó là công việc nhiều hơn nữa và bạn nên bây giờ một vài điều về thạch anh 2d, nhưng kết quả là chính xác những gì bạn muốn. Nếu không, bạn có thể chỉ cần thêm một hình ảnh, nhưng tôi đặt trước vào lớp con UIButton, vì nó rất linh hoạt về kích thước của nút, nó tổng quát hơn.

79

Chỉ có một lỗi nhỏ trong câu hỏi làm cho bóng không được hiển thị: masksToBounds:YES cũng che dấu bóng! Đây là mã chính xác:

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); 

Thật không may, điều này có nghĩa là chúng tôi không thể che giấu nội dung và có bóng cùng một lúc mà không có thủ thuật.

Hãy nhớ #import <QuartzCore/QuartzCore.h>.

37

Đây là lớp con không chỉ tạo bóng đổ, mà nút này hoạt ảnh khi bạn nhấn vào nó.

// 
// 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 
+0

Đơn giản và có thể sử dụng, cảm ơn bạn – webo80

49

Dưới đây là một giải pháp dễ dàng nếu bạn đang sử dụng một 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; 

Chỉ cần nhận nó làm việc, và figured nó đã được giá trị niêm yết. Hy vọng rằng Giúp!

+1

giải pháp rất thanh lịch! Cảm ơn! –

+1

hoạt động hoàn hảo ... ngay cả khi tôi không nhập "" Tác phẩm của nó cho tôi .. về người khác mà tôi không biết. – g212gs

+0

nếu bạn làm theo cách này sẽ làm chậm trễ cảm ứng trợ giúp của bạn trong bàn phím tùy chỉnh – TomSawyer

2

Bạn có thể tạo một lớp con và cấu hình các giá trị của nó trong Xcode

Dưới đây là một ví dụ:

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 
    } 
} 


} 
Các vấn đề liên quan