我想給一個imageView同時帶圓角的陰影,但是我失敗了。如何在SWIFT3.0.1陰影中同時提供一個帶有圓角的陰影
3
A
回答
0
解決方案是創建兩個單獨的視圖。一個用於陰影,另一個用於圖像本身。在imageView上clipToBounds圖層,以便正確添加角半徑。
將imageView放在shadowView的頂部,你已經有了解決方案!
3
這裏是我的解決方案
基本思想:
- 使用額外的視圖(比如AView)的圖像視圖的超視圖(那些對你願意有shado觀點)和該視圖類分配給DGShadoView
- 引腳圖片以AView(即超級視圖)由左,右,頂部和底部具有恆定5
- 從storybosrd的屬性檢查器設置回AView的底色清晰的彩色這是很重要的
裏面的想法:這裏我們使用的Aview幾乎邊境貝塞爾路徑並將所有圓角屬性和陰影屬性設置爲該路徑,並且我們將我們的目標圖像視圖與該路徑綁定在一起
@IBDesignable
class DGShadoView:UIView {
override func draw(_ rect: CGRect) {
self.rect = rect
decorate(rect: self.rect)
}
func decorate(rect:CGRect) {
//self.backgroundColor = UIColor.clear
//IMPORTANT: dont forgot to set bg color of your view to clear color from story board's property inspector
let ref = UIGraphicsGetCurrentContext()
let contentRect = rect.insetBy(dx: 5, dy: 5);
/*create the rounded oath and fill it*/
let roundedPath = UIBezierPath(roundedRect: contentRect, cornerRadius: 5)
ref!.setFillColor("your color for background".cgColor)
ref!.setShadow(offset: CGSize(width:0,height:0), blur: 5, color: "your color for shado".cgColor)
roundedPath.fill()
/*draw a subtle white line at the top of view*/
roundedPath.addClip()
ref!.setStrokeColor(UIColor.red.cgColor)
ref!.setBlendMode(CGBlendMode.overlay)
ref!.move(to: CGPoint(x:contentRect.minX,y:contentRect.minY+0.5))
ref!.addLine(to: CGPoint(x:contentRect.maxX,y:contentRect.minY+0.5))
}
}
更新
擴展方法
還有一個辦法。只需用空白粘貼一個類,然後粘貼以下UIImageView擴展代碼,將此子類分配給您在其上映射的ImageView。
import UIKit
class DGShadowView: UIImageView {
@IBInspectable var intensity:Float = 0.2{
didSet{
setShadow()
}
}
override func layoutSubviews()
{
super.layoutSubviews()
setShadow()
}
func setShadow(){
let shadowPath = UIBezierPath(rect: bounds)
layer.masksToBounds = false
layer.shadowColor = UIColor.black.cgColor
layer.shadowOffset = CGSize(width: 0.0, height: 0.3)
layer.shadowOpacity = intensity
layer.shadowPath = shadowPath.cgPath
}
}
相關問題
- 1. 帶圓角和陰影的圖像
- 2. 帶陰影的圓形UIView?
- 3. 帶圓角矩形和陰影的UIView:陰影出現在矩形上方
- 4. 如何在UITableViewCell中實現帶圓角的陰影?
- 5. 帶有陰影CSS的圓3
- 6. 僅限於角落的圓角陰影
- 7. 在Adobe Photoshop中投影陰影角度陰影
- 8. 如何製作帶圓角或陰影的舞臺?
- 9. 如何提供陰影按鈕
- 10. 帶陰影的UIView
- 11. 用於圓角的jQuery陰影
- 12. 對透明UITableView和圓角的陰影
- 13. 帶有陰影和角半徑的UITableView(附帶電影示例)
- 14. 帶有圓角和內部陰影的ImageView
- 15. 如何設置一個盒子陰影的div元素的所有方面,左陰影,右陰影,頂部陰影,底部陰影?
- 16. UIView的圓角和圓角陰影葉4個邊緣
- 17. 帶有浮動陰影的動畫陰影效果
- 18. 自定義NSView與圓角和陰影
- 19. 可以在IE中設計一個帶css3陰影+漸變+圓角的div嗎?
- 20. iphone卓爾圓陰影
- 21. 沒有陰影
- 22. 沒有陰影
- 23. 如何在Chart.js中過濾陰影或陰影?
- 24. OpenGL ES 2D陰影的陰影
- 25. 如何在圓角的UITextField上添加陰影?
- 26. 繪製一個圓角的UIView梯度和陰影
- 27. 帶陰影的視圖
- 28. 帶有陰影和圓形邊緣的java swing中的圖像
- 29. UILabel帶圓角,陰影和背景圖案
- 30. 使用CSS3盒子陰影的三面一個像素陰影