简体   繁体   English

UIView的阴影,如建筑物的阴影

[英]Shadow for UIView like the shadow of a building

I need to add a shadow to a UIView . 我需要向UIView添加阴影。 Imagine it like a building seen from the above. 想象它就像是从上方看的建筑物。

Please look at this image, 请看这张图片,

在此处输入图片说明

My code: 我的代码:

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

This code does not get the result I am looking for. 这段代码没有得到我想要的结果。

What am I missing? 我想念什么?

You can try setting a shadow path. 您可以尝试设置阴影路径。 For example: 例如:

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;

A detailed tutorial can be found here: http://nachbaur.com/blog/fun-shadow-effects-using-custom-calayer-shadowpaths 可以在这里找到详细的教程: http : //nachbaur.com/blog/fun-shadow-effects-using-custom-calayer-shadowpaths

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM