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

标签 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;

这段代码没有得到我正在寻找的结果。

我错过了什么?

最佳答案

您可以尝试设置影子路径。例如:

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

关于ios - UIView 的阴影就像建筑物的阴影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22711018/

相关文章:

ios - 带有表情符号设置 : cursorPosition is not correct 的 UITextView.text

ios - 将本地视频嵌入到 UIView 中

iphone - UIView的位置

wpf 弹出阴影未显示

c# - 我的软阴影代码有什么问题?

javascript - 在函数中将变量赋值给自身

ios - 为我的数组字典创建唯一键

ios - UILabel文本问题

iOS 5/6 : low volume after first usage of CoreAudio

UIView 通过某些事件