我只是想給圖片添加一個陰影,這個陰影不僅僅是矩形的,我想應用和圖片一樣的遮罩效果。
在 stack overflow 上,我發現了同樣的問題,但答案是由 objective-c 語言建議的 - 
現在,只用面具:
還有,用面具和陰影:
最后,沒有遮擋和陰影:
下面是一個視圖控制器的例子:
class MaskShadowViewController。UIViewController {
let testView = ShadowMaskImageView()
override func viewDidLoad() {
super.viewDidLoad()
//確保我們可以加載圖片。
guard let img = UIImage(命名。"sample") else {
fatalError("Could not load sample image!!")
}
//設定影像
testView.image = img
//加入testView并設定其高度等于原始圖片的比例。
testView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(testView)
let g = view.safeAreaLayoutGuide
NSLayoutConstraint.activated([
testView.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 20.0)。
testView.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -20.0)。)
testView.heightAnchor.constraint(equalTo: testView.widthAnchor, multiplier: img.size.height / img.size.width) 。
testView.centerYAnchor.constraint(equalTo: g.centerYAnchor)。
])
//>如果我們想看到自定義視圖的框架,請取消下面的注釋。
//let frameView = UIView()
//frameView.translatesAutoresizingMaskIntoConstraints = false。
//view.addSubview(frameView)
//NSLayoutConstraint.activate([/span>)
// frameView.topAnchor.constraint(equalTo: testView.topAnchor, constant: 0.0),
// frameView.leadingAnchor.constraint(equalTo: testView.leadingAnchor, constant: 0.0) ,
// frameView.trailingAnchor.constraint(equalTo: testView.trailingAnchor, constant: 0.0) ,
// frameView.bottomAnchor.constraint(equalTo: testView.bottomAnchor, constant: 0.0) ,
//])
//
//frameView.layer.borderWidth = 1
//frameView.layer.borderColor = UIColor.red.cgColor。
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/307290.html
標籤:




