我正在使用蒙版。
我的代码:
class ViewController: UIViewController {
@IBOutlet weak var firstImageView: UIImageView!
@IBOutlet weak var seconImageView: UIImageView!
let maskView = UIImageView()
let maskView2 = UIImageView()
override func viewDidLoad() {
super.viewDidLoad()
maskView.image = UIImage(named: "Up")
maskView2.image = UIImage(named: "Down")
firstImageView.mask = maskView
seconImageView.mask = maskView2
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
maskView.frame = firstImageView.bounds
maskView2.frame = seconImageView.bounds
}
}
UIImageView代码:
UIImageView类。我看不到这个设计。我能怎么做 ?我添加了自定义类,但没有用
@IBDesignable
class FirstImageView: UIImageView {
var maskkView: UIImageView!
override init(frame: CGRect) {
super.init(frame: frame)
layoutSubviews()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
layoutSubviews()
}
override func layoutSubviews() {
super.layoutSubviews()
maskkView.image = UIImage(named: "mask")
mask = maskkView
maskkView.frame = bounds
}
}
故事板:
Simulator:
我如何在故事板上使用@designable看到此设计。
我添加了自定义类,但没有用
[向您保证,layoutSubviews
UIImageView的IBDesignable
中的遮罩确实起作用。示例:
@IBDesignable
class MyImageView : UIImageView {
override func layoutSubviews() {
super.layoutSubviews()
let lay = CAShapeLayer()
lay.frame = self.bounds
lay.path = UIBezierPath(ovalIn: self.bounds).cgPath
self.layer.mask = lay
}
}
结果:
I 怀疑您的代码有问题,是您试图从IBDesignable
代码中的捆绑包中加载图像;我的猜测是那将行不通。 IBDesignable
代码只是在情节提要渲染时运行的某些内容;它无法像真正运行的应用程序那样运行。