2010-02-23 13 views
63

मैं 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); 
+0

कोर एनिमेशन गाइड, http://developer.apple.com/mac/library/documentation /cocoa/conceptual/CoreAnimation_guide/Articles/LayerVisProps.html, कहता है: आईफोन ओएस नोट: एक प्रदर्शन विचार के रूप में, आईफोन ओएस छाया रंग, छायाऑफसेट, छाया ओपेसिटी, और छाया रेडियस गुणों का समर्थन नहीं करता है। डैंग। –

+0

यह गुण अब आईओएस 3.2 के बाद से समर्थित हैं। सम्मान, – Quentin

उत्तर

2

आप UIButton को उपclass कर सकते हैं और drawRect: विधि को ओवरराइट कर सकते हैं और मैन्युअल रूप से एक ड्रॉप छाया जोड़ सकते हैं। यह बहुत अधिक काम है और अब आपको क्वार्ट्ज 2 डी के बारे में कुछ चीजें चाहिए, लेकिन परिणाम वही है जो आप चाहते हैं। अन्यथा आप केवल एक छवि जोड़ सकते हैं, लेकिन मैं UIButton को उपclass करना पसंद करता हूं, क्योंकि यह बटन के आकार के संबंध में बहुत लचीला है, यह अधिक सामान्य है।

79

प्रश्न में केवल एक छोटी गलती है जो छाया को प्रदर्शित नहीं करती है: 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> को याद रखें।

37

यहां एक उप-वर्ग है जो न केवल ड्रॉप छाया बनाता है, लेकिन जब आप इसे दबाते हैं तो बटन एनिमेट हो जाता है।

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

सरल और प्रयोग योग्य, धन्यवाद – webo80

49

यहाँ एक आसान उपाय है कि यदि आप एक 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; 

बस यह काम कर रहा है, और लगा यह पोस्टिंग के लायक था। उम्मीद है की वो मदद करदे!

+1

बहुत ही सुरुचिपूर्ण समाधान! धन्यवाद! –

+1

पूरी तरह से काम करता है ... भले ही मैं आयात नहीं करता "" मेरे लिए यह काम .. अन्य व्यक्ति के बारे में मुझे नहीं पता। – g212gs

+0

यदि आप ऐसा करते हैं तो इस तरह से कस्टम कीबोर्ड में आपकी सहायक स्पर्श अंतराल बन जाएगी – TomSawyer

2

आप एक उपवर्ग बना सकते हैं और 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 
    } 
} 


} 
संबंधित मुद्दे