2014-03-28 55 views
0

的影子,我需要一個陰影添加到UIView。想象它就像從上面看到的建築物。陰影的UIView的像一個建築

請看看這個圖像,

enter image description here

我的代碼:

//creating the view 
UIView * view = [[UIView alloc] initWithFrame:CGRectMake(200, 200, 100, 100)]; 
[view setBackgroundColor:[UIColor whiteColor]]; 
[[view layer] setBorderColor:[[UIColor blackColor] CGColor]]; 
[[view layer] setBorderWidth:1.0f]; 

//adding the view to viewcontroller 
[self.view addSubview:view]; 

//adding shadow to the view 
view.layer.masksToBounds = NO; 
view.layer.cornerRadius = 50; // if you like rounded corners 
view.layer.shadowOffset = CGSizeMake(0, 0); 
view.layer.shadowRadius = 10; 
view.layer.shadowOpacity = 1; 

此代碼沒有得到我要找的結果。

我錯過了什麼?

+0

我不認爲這是直接可以那樣做。你必須找到一種拙劣的方式,比如創建一個「陰影視圖」。 –

+0

如果我的答案有幫助請標記爲答案。 – Imran

回答

1

你可以嘗試設置了陰影路徑。例如:

UIBezierPath *path = [UIBezierPath bezierPath]; 
[path moveToPoint:CGPointMake(size.width * 0.33f, size.height * 0.66f)]; 
[path addLineToPoint:CGPointMake(size.width * 0.66f, size.height * 0.66f)]; 
[path addLineToPoint:CGPointMake(size.width * 1.15f, size.height * 1.15f)]; 
[path addLineToPoint:CGPointMake(size.width * -0.15f, size.height * 1.15f)]; 
imgView.layer.shadowPath = path.CGPath; 

一個詳細的教程可以在這裏找到:http://nachbaur.com/blog/fun-shadow-effects-using-custom-calayer-shadowpaths