UIView的阴影就像建筑物的阴影

时间:2014-03-28 11:02:18

标签: ios uiview shadow

我需要为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;

此代码无法获得我要查找的结果。

我错过了什么?

1 个答案:

答案 0 :(得分: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