【问题标题】:Shadow for UIView like the shadow of a buildingUIView 的阴影就像建筑物的阴影
【发布时间】:2014-03-28 11:02:18
【问题描述】:

我需要为UIView 添加阴影。把它想象成从上面看到的建筑物。

请看这张图片,

我的代码:

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

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

我错过了什么?

【问题讨论】:

  • 我不认为直接可以这样做。您必须找到一种 hacky 方式,例如创建“阴影视图”。
  • 如果我的回答有帮助,请标记为回答。

标签: ios uiview shadow


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-31
    • 2014-07-08
    • 2011-11-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多