经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » iOS » 查看文章
UIView设置阴影
来源:cnblogs  作者:鸿鹄当高远  时间:2019/6/3 8:56:41  对本文有异议

 

UI设计师有时候希望我们的产品比较酷。

阴影是他们喜欢的效果之一。

 

怎么设置阴影呢?

 

1、设置一个四边都相同的阴影

  1. UIImageView *testImgView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 200, 100)];
  2. [testImgView setBackgroundColor:[UIColor yellowColor]];
  3. // 阴影颜色
  4. testImgView.layer.shadowColor = [UIColor blackColor].CGColor;
  5. // 阴影偏移,默认(0, -3)
  6. testImgView.layer.shadowOffset = CGSizeMake(0,0);
  7. // 阴影透明度,默认0
  8. testImgView.layer.shadowOpacity = 0.5;
  9. // 阴影半径,默认3
  10. testImgView.layer.shadowRadius = 5;
  11. [self.view addSubview:testImgView];

效果如图:

 

2、设置单边阴影

 

  1. //单边阴影
  2. UILabel *testLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 300, 200, 100)];
  3. [testLabel setBackgroundColor:[UIColor yellowColor]];
  4. // 阴影颜色
  5. testLabel.layer.shadowColor = [UIColor blackColor].CGColor;
  6. // 阴影偏移,默认(0, -3)
  7. testLabel.layer.shadowOffset = CGSizeMake(0,0);
  8. // 阴影透明度,默认0
  9. testLabel.layer.shadowOpacity = 0.5;
  10. // 阴影半径,默认3
  11. testLabel.layer.shadowRadius = 5;
  12. // 单边阴影 顶边
  13. float shadowPathWidth = testLabel.layer.shadowRadius;
  14. CGRect shadowRect = CGRectMake(-shadowPathWidth/2.0, 0-shadowPathWidth/2.0, testLabel.bounds.size.width+shadowPathWidth, shadowPathWidth);
  15. UIBezierPath *path = [UIBezierPath bezierPathWithRect:shadowRect];
  16. testLabel.layer.shadowPath = path.CGPath;
  17. [self.view addSubview:testLabel];

 

效果如下:

 

3、和阴影相关的属性

 

  1. /** Shadow properties. **/
  2.  
  3. /* The color of the shadow. Defaults to opaque black. Colors created
  4. * from patterns are currently NOT supported. Animatable. */
  5. @property(nullable) CGColorRef shadowColor;
  6. /* The opacity of the shadow. Defaults to 0. Specifying a value outside the
  7. * [0,1] range will give undefined results. Animatable. */
  8. @property float shadowOpacity;
  9. /* The shadow offset. Defaults to (0, -3). Animatable. */
  10. @property CGSize shadowOffset;
  11. /* The blur radius used to create the shadow. Defaults to 3. Animatable. */
  12. @property CGFloat shadowRadius;
  13. /* When non-null this path defines the outline used to construct the
  14. * layer's shadow instead of using the layer's composited alpha
  15. * channel. The path is rendered using the non-zero winding rule.
  16. * Specifying the path explicitly using this property will usually
  17. * improve rendering performance, as will sharing the same path
  18. * reference across multiple layers. Upon assignment the path is copied.
  19. * Defaults to null. Animatable. */
  20. @property(nullable) CGPathRef shadowPath;

 

原文链接:http://www.cnblogs.com/jukaiit/p/10940062.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号