Detecting tap inside a bezier path

后端 未结 4 640
生来不讨喜
生来不讨喜 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:50

    Detecting tap inside a bezier path in swift :-

    It's simple in latest swift ,follow these steps and you will get your UIBezierPath touch event.

    Step 1 :- Initialize Tap Event on view where your UIBeizerPath Added.

    ///Catch layer by tap detection let tapRecognizer:UITapGestureRecognizer = UITapGestureRecognizer.init(target: self, action: #selector(YourClass.tapDetected(_:))) viewSlices.addGestureRecognizer(tapRecognizer)

    Step 2 :- Make "tapDetected" method

      //MARK:- Hit TAP
    public func tapDetected(tapRecognizer:UITapGestureRecognizer){
        let tapLocation:CGPoint = tapRecognizer.locationInView(viewSlices)
        self.hitTest(CGPointMake(tapLocation.x, tapLocation.y))
    
    
    }
    

    Step 3 :- Make "hitTest" final method

      public func hitTest(tapLocation:CGPoint){
            let path:UIBezierPath = yourPath
            if path.containsPoint(tapLocation){
                //tap detected do what ever you want ..;)
            }else{
                 //ooops you taped on other position in view
            }
        }
    



    Update: Swift 4

    Step 1 :- Initialize Tap Event on view where your UIBeizerPath Added.

    ///Catch layer by tap detection
    let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(YourClass.tapDetected(tapRecognizer:)))
    viewSlices.addGestureRecognizer(tapRecognizer)
    

    Step 2 :- Make "tapDetected" method

    public func tapDetected(tapRecognizer:UITapGestureRecognizer){
        let tapLocation:CGPoint = tapRecognizer.location(in: viewSlices)
        self.hitTest(tapLocation: CGPoint(x: tapLocation.x, y: tapLocation.y))
    }
    

    Step 3 :- Make "hitTest" final method

    private func hitTest(tapLocation:CGPoint){
        let path:UIBezierPath = yourPath
    
        if path.contains(tapLocation){
            //tap detected do what ever you want ..;)
        }else{
            //ooops you taped on other position in view
        }
    }
    

提交回复
热议问题