2011-11-09 31 views
10

enter image description hereiphone - 我怎樣才能產生一個圓角和陰影的imageview?

東西跟上面一樣嗎?

我知道如何製作圓角:

imageView.layer.cornerRadius = 10; 
imageView.layer.shouldRasterize = YES; 
imageView.layer.masksToBounds = YES; 

對於影子,我曾嘗試

imageView.layer.shadowOffset = CGSizeMake(1, 1); 
imageView.layer.shadowRadius = 5; 
imageView.layer.shadowOpacity = 0.4; 
imageView.layer.shadowColor = [UIColor blackColor].CGColor; 
imageView.layer.shouldRasterize = YES; 

imageView.layer.masksToBounds = YES;從圓角殺死的陰影。

另一個問題是如何產生一個完全像圖像中顯示的陰影?我在Photoshop中製作了這個圖像,我用120度作爲光線的方向。但是,如果我使用上面的代碼,並關閉maskToBounds,我可以看到陰影,它很醜陋。

或者我可以在Photoshop中產生一個圓角+陰影圖像框架並將該框架應用到我的應用程序中的每個圖像?我認爲這會帶來更好的表現。如果所有圖像都處於滾動狀態,則陰影和轉動圖像會產生可怕的表現。

謝謝

回答

0

我會使用兩個圖像視圖。具有陰影的背景png(可以重新用於每個圖像)和具有圓角的前景png圖像。

4

試試這個:

CALayer *sublayer = [CALayer layer]; 
sublayer.backgroundColor = [UIColor blueColor].CGColor; 
sublayer.shadowOffset = CGSizeMake(0, 3); 
sublayer.shadowRadius = 5.0; 
sublayer.shadowColor = [UIColor blackColor].CGColor; 
sublayer.shadowOpacity = 0.8; 
sublayer.frame = CGRectMake(30, 30, 128, 192); 
sublayer.borderColor = [UIColor blackColor].CGColor; 
sublayer.borderWidth = 2.0; 
sublayer.cornerRadius = 10.0; 
[self.view.layer addSublayer:sublayer]; 

CALayer *imageLayer = [CALayer layer]; 
imageLayer.frame = sublayer.bounds; 
imageLayer.cornerRadius = 10.0; 
imageLayer.contents = (id) [UIImage imageNamed:@"BattleMapSplashScreen.jpg"].CGImage; 
imageLayer.masksToBounds = YES; 
[sublayer addSublayer:imageLayer]; 

並採取看看original source

-1

您可能需要使用不同的層的陰影,並保持masksToBounds和舍入代碼。在這個例子中imageView是名稱要陰影圖像視圖和輪:

CALayer *shadowLayer = [[[CALayer alloc] init] autorelease]; 
sublayer.frame = imageView.bounds; 
sublayer.masksToBounds = NO; 
sublayer.shadowOffset = CGSizeMake(1, 1); 
sublayer.shadowRadius = 5; 
sublayer.shadowOpacity = 0.4; 
sublayer.shadowColor = [UIColor blackColor].CGColor; 
sublayer.shouldRasterize = YES; 

[imageView.layer insertSublayer:shadowLayer atIndex:0]; // Put it underneath the image 

這應該給你的影子。現在爲了避免緩慢的重新計算,我建議在圖像中創建一個UIImage。看到這個鏈接:Create UIImage from shadowed view while retaining alpha?

希望這會有所幫助!

相關問題