view圆角、阴影

2018-10-25  本文已影响0人  凨弥

项目中用到卡片模式,有阴影效果,记录一下
切角一般用到 layer.masksToBounds = trueUIBezierPath

shadowColor: 阴影颜色
shadowRadius: 阴影切角
shadowOffset: 偏移量 与 shadowRadius配合使用,如shadowRadius = 5 (0,0)代表四边都有阴影,x代表左右偏移,y代表上下偏移
shadowOpacity: 透明度


 let tsView = UIView(frame: CGRect(x: 37, y: 100, width: 300, height: 300))
        tsView.backgroundColor = #colorLiteral(red: 0.9529411793, green: 0.6862745285, blue: 0.1333333403, alpha: 1)
        tsView.layer.cornerRadius = 10
        tsView.layer.shadowColor = #colorLiteral(red: 0, green: 1, blue: 0.8727540374, alpha: 1).cgColor
        tsView.layer.shadowOffset = CGSize(width: 10, height: 10)
        tsView.layer.shadowRadius = 5
        tsView.layer.shadowOpacity = 0.9
        tsView.layer.borderWidth = 0.5
        tsView.layer.borderColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1).cgColor
        view.addSubview(tsView)
QQ20181025-001733@2x.png QQ20181025-001751@2x.png

注意

遇到的坑,masksToBounds 属性设置为 true 是,阴影效果是出不来的
官方说明:

/* When true an implicit mask matching the layer bounds is applied to
     * the layer (including the effects of the `cornerRadius' property). If
     * both `mask' and `masksToBounds' are non-nil the two masks are
     * multiplied to get the actual mask values. Defaults to NO.
     * Animatable. */

目前发现UIImageView显示的图片对于切角不好使!

上一篇 下一篇

猜你喜欢

热点阅读