天天看點

layer層CornerRadius影響shadow的實作

手動設定視圖的圓角效果(cornerRadius),會屏蔽視圖的投影效果(Shadow)。問題總是在解決後,才發現僅僅隻是個紙老虎!展示下代碼,一看就明白。
let imageShadow = UIImageView.init(frame: CGRectMake(50, 50, UIScreen.mainScreen().bounds.width - 100, UIScreen.mainScreen().bounds.width - 100))
        self.view.addSubview(imageShadow)
        imageShadow.image = UIImage(named: "IMG_0412.JPG")
        imageShadow.layer.shadowColor = UIColor.init(red: 238/225.0, green: 100/255.0, blue: 81/255.0, alpha: 1).CGColor
        imageShadow.layer.shadowOffset = CGSizeMake(0, 3.0)
        imageShadow.layer.shadowOpacity = 0.2   // 透明度
        imageShadow.layer.shadowRadius = 6.0    // 半徑

        /**
         注意點: 控件的圓角屬性,會影響控件的投影的實作。例如:加入下面的設定圓角屬性的代碼,将看不到控件的投影效果。
         */
//        imageShadow.layer.cornerRadius = 5
//        imageShadow.layer.masksToBounds = true           

效果圖對比

layer層CornerRadius影響shadow的實作