iOS invert mask in drawRect

前端 未结 7 2008
名媛妹妹
名媛妹妹 2020-12-08 07:54

With the code below, I am successfully masking part of my drawing, but it\'s the inverse of what I want masked. This masks the inner portion of the drawing, where I would li

7条回答
  •  -上瘾入骨i
    2020-12-08 08:27

    In order to invert mask you can to something like this

    1. Here I have mask of crossed rectancles like

       let crossPath =  UIBezierPath(rect: cutout.insetBy(dx: 30, dy: -5))
                crossPath.append(UIBezierPath(rect: cutout.insetBy(dx: -5, dy: 30)))
                  let crossMask = CAShapeLayer()
                  crossMask.path = crossPath.cgPath
                  crossMask.backgroundColor = UIColor.clear.cgColor
                  crossMask.fillRule = .evenOdd
      
    2. And here I add 3rd rectancle around my crossed rectancles so using .evenOdd it takes area that is equal to (New Rectancle - Old Crossed Rectangle) so in other words outside area of crossed rectancles

        let crossPath = UIBezierPath(rect: cutout.insetBy(dx: -5, dy: -5) )
          crossPath.append(UIBezierPath(rect: cutout.insetBy(dx: 30, dy: -5)))
          crossPath.append(UIBezierPath(rect: cutout.insetBy(dx: -5, dy: 30)))
          let crossMask = CAShapeLayer()
          crossMask.path = crossPath.cgPath
          crossMask.backgroundColor = UIColor.clear.cgColor
          crossMask.fillRule = .evenOdd
      

提交回复
热议问题