web-dev-qa-db-fra.com

Utiliser un UIView en tant que masque dans un autre UIView sur Swift

Quelqu'un peut-il m'aider s'il vous plaît avec quelque chose? J'ai fait beaucoup de recherches et rien trouvé. Il s'agit de masquer.

Je développe une application sur xCode avec Swift. Mon écran a une image d'un animal (point de vente "backImage"), sur cette image, j'ai une vue (point de vente "coverView"), en noir, qui couvre tout l'écran. Si loin, vous ne pouvez pas voir l'image de l'animal, car le "coverView" est devant. Ensuite, j'ai une autre vue (sortie "maskView") qui est plus petite et qui est sur la grande vue "coverView". Ce que je veux, c'est utiliser ce masque "maskView" comme masque et voir ainsi le "backImage" à travers, comme une fenêtre.

Y a-t-il quelqu'un qui puisse comprendre cela?

Voici mon écran, je veux voir le personnage féminin derrière la grande vue grise à travers la plus petite vue blanche:

 enter image description here

Merci.

Cordialement, Renan Gaspar.

5
DevRenanGaspar

Vous pouvez définir la propriété alpha à partir de votre vue de masque et ajouter devant l'autre vue, par exemple:

let maskView = UIView()
maskView.backgroundColor = UIColor(white: 0, alpha: 0.5) //you can modify this to whatever you need
maskView.frame = CGRect(x: 0, y: 0, width: imageView.frame.width, height: imageView.frame.height)

yourView.addSubview(maskView)

EDIT: Maintenant que vous avez modifié votre question avec une image, je vois maintenant ce dont vous avez besoin, alors voici comment vous pouvez y parvenir.

func setMask(with hole: CGRect, in view: UIView){

    // Create a mutable path and add a rectangle that will be h
    let mutablePath = CGMutablePath()
    mutablePath.addRect(view.bounds)
    mutablePath.addRect(hole)

    // Create a shape layer and cut out the intersection
    let mask = CAShapeLayer()
    mask.path = mutablePath
    mask.fillRule = kCAFillRuleEvenOdd

    // Add the mask to the view
    view.layer.mask = mask

}

Avec cette fonction, tout ce dont vous avez besoin est d'avoir une vue et de créer une forme qui deviendra un trou dans cette vue, par exemple:

// Create the view (you can also use a view created in the storyboard)
let newView = UIView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height))
newView.backgroundColor = UIColor(white: 0, alpha: 1)

// You can play with these values and find one that fills your need
let rectangularHole = CGRect(x: view.bounds.width*0.3, y: view.bounds.height*0.3, width: view.bounds.width*0.5, height: view.bounds.height*0.5)

// Set the mask in the created view        
setMask(with: rectangularHole, in: newView)
8
Alexandre Lara

Merci @ Alexandre Lara! Tu l'as fait!

Voici la solution:

@IBOutlet weak var windowView: UIView!
@IBOutlet weak var bigCoverView: UIView!

func setMask(with hole: CGRect, in view: UIView){

    // Create a mutable path and add a rectangle that will be h
    let mutablePath = CGMutablePath()
    mutablePath.addRect(view.bounds)
    mutablePath.addRect(hole)

    // Create a shape layer and cut out the intersection
    let mask = CAShapeLayer()
    mask.path = mutablePath
    mask.fillRule = kCAFillRuleEvenOdd

    // Add the mask to the view
    view.layer.mask = mask

}

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    let rectangularHole = windowView.frame.integral

    // Set the mask in the created view
    setMask(with: rectangularHole, in: bigCoverView!)

}
0
DevRenanGaspar