Detecting tap inside a bezier path

后端 未结 4 633
生来不讨喜
生来不讨喜 2020-12-11 02:00

I have a UIView which is added as a subview to my view controller. I have drawn a bezier path on that view. My drawRect implementation is below

- (void)drawR         


        
4条回答
  •  [愿得一人]
    2020-12-11 02:48

    A solution in Swift 3.1 (porting over the Apple recommended solution from here)

    func containsPoint(_ point: CGPoint, path: UIBezierPath, inFillArea: Bool) -> Bool {
    
            UIGraphicsBeginImageContext(self.size)
    
            let context: CGContext? = UIGraphicsGetCurrentContext()
            let pathToTest = path.cgPath
            var isHit = false
    
            var mode: CGPathDrawingMode = CGPathDrawingMode.stroke
    
            if inFillArea {
    
                // check if UIBezierPath uses EO fill
                if path.usesEvenOddFillRule {
                    mode = CGPathDrawingMode.eoFill
                } else {
                    mode = CGPathDrawingMode.fill
                }
            } // else mode == stroke
    
            context?.saveGState()
            context?.addPath(pathToTest)
    
            isHit = (context?.pathContains(point, mode: mode))!
            context?.restoreGState()
    
            return isHit
            }
    

提交回复
热议问题