2013-07-18 36 views
1

我在網上看過,但是我的結果主要包含「如何做一個shadowOffset」。我需要將「陰影,邊框和CornerRadius」應用於我的視圖中的多個對象。我只想看看是否有更有效的方法?或者,如果能夠使它更有條理。繼承我的一些代碼。請記住,我有這樣的多個視圖,所以這段代碼佔用的空間量非常煩人。UIViewController上有多個shadowOffset

topView.layer.cornerRadius = 3; 
topView.layer.masksToBounds = YES; 
topView.layer.borderColor= [UIColor lightGrayColor].CGColor; 
topView.layer.borderWidth = 0.5f; 

bottomView.layer.cornerRadius = 3; 
bottomView.layer.masksToBounds = YES; 
bottomView.layer.borderColor= [UIColor lightGrayColor].CGColor; 
bottomView.layer.borderWidth = 0.5f; 

eventName.layer.masksToBounds = NO; 
eventName.layer.shadowColor = [UIColor blackColor].CGColor; 
eventName.layer.shadowOpacity = 0.5; 
eventName.layer.shadowRadius = 2; 
//(right,down) also (-right,-down) 
eventName.layer.shadowOffset = CGSizeMake(0.0f, 0.8f); 

addressLabel.layer.masksToBounds = NO; 
addressLabel.layer.shadowColor = [UIColor blackColor].CGColor; 
addressLabel.layer.shadowOpacity = 0.5; 
addressLabel.layer.shadowRadius = 2; 
//(right,down) also (-right,-down) 
addressLabel.layer.shadowOffset = CGSizeMake(0.0f, 0.8f); 

dateLabel.layer.masksToBounds = NO; 
dateLabel.layer.shadowColor = [UIColor blackColor].CGColor; 
dateLabel.layer.shadowOpacity = 0.5; 
dateLabel.layer.shadowRadius = 2; 
//(right,down) also (-right,-down) 
dateLabel.layer.shadowOffset = CGSizeMake(0.0f, 0.8f); 

typeLabel.layer.masksToBounds = NO; 
typeLabel.layer.shadowColor = [UIColor blackColor].CGColor; 
typeLabel.layer.shadowOpacity = 0.5; 
typeLabel.layer.shadowRadius = 2; 
//(right,down) also (-right,-down) 
typeLabel.layer.shadowOffset = CGSizeMake(0.0f, 0.8f); 

eventCaption.layer.masksToBounds = NO; 
eventCaption.layer.shadowColor = [UIColor blackColor].CGColor; 
eventCaption.layer.shadowOpacity = 0.5; 
eventCaption.layer.shadowRadius = 2; 
//(right,down) also (-right,-down) 
eventCaption.layer.shadowOffset = CGSizeMake(0.0f, 0.8f); 

回答

1

如果你的大多數陰影是相同的,你可以選擇遍歷數組(或一組,因爲你真的不關心它應用到項目的順序)你的看法應用相同的陰影? 也就是說,我在你粘貼的代碼中看到2種陰影,lightGray和黑色。 你可以這樣做:

NSArray * blackShadowItems = @[eventName, addressLabel, dateLabel]; 
for (UIView * view in blackShadowItems) { 
    view.layer.masksToBounds = NO; 
    view.layer.shadowColor = [UIColor blackColor].CGColor; 
    view.layer.shadowOpacity = 0.5; 
    view.layer.shadowRadius = 2; 
    //(right,down) also (-right,-down) 
    view.layer.shadowOffset = CGSizeMake(0.0f, 0.8f); 
} 

或聲明另一個功能:

- (void)setBlackShadow:(UIView *)view { 
    view.layer.masksToBounds = NO; 
    view.layer.shadowColor = [UIColor blackColor].CGColor; 
    view.layer.shadowOpacity = 0.5; 
    view.layer.shadowRadius = 2; 
    //(right,down) also (-right,-down) 
    view.layer.shadowOffset = CGSizeMake(0.0f, 0.8f); 
} 

您可以結合這兩種解決方案。

最後,請注意,如果這些視圖是在.xib文件中聲明的,則可以根據要設置的陰影類型聲明IBOutletCollection以重新分組視圖。 這與自己聲明你的NSArray或NSSet有些相似。

+0

非常感謝!我甚至沒有想過要做一個陣列。我認爲有某種默認方法。再次感謝! –