-1

I'm using masking.

My Code:

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 Code:

UIImageView class. I can't see this design. How can I do ? I added custom class but didn't work

@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
    }

}

Storyboard:

enter image description here

Simulator:

enter image description here

How can i see with @designable this design on storyboard.

1 Answers1

0

I added custom class but didn't work

I assure you that masking in layoutSubviews of an IBDesignable UIImageView does work. Example:

@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
    }
}

Result:

enter image description here

I suspect that the problem with your code is that you are attempting to load an image from the bundle with UIImage(named:) in your IBDesignable code; my guess is that that isn't going to work. IBDesignable code is just some stuff that runs at storyboard rendering time; it cannot behave as if it were truly the running app.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • thanks sir :) but I'm using another triangle png image for masking. I'm trying to make a triangle. I can't do. How can I do ? –  Jan 28 '20 at 19:33
  • If I'm right that you can't load an image in an IBDesignable's code, you should do what I did in my example: draw the triangle shape itself as a UIBezierPath, in code. – matt Jan 29 '20 at 02:11