问题描述:

I have come to this problem and there's no solution to my knowledge. The solution must satisfy 2 requirements:

1. Catch TouchUpInside event, not TouchUpOutside. I need to take action as soon as finger touches the screen, not on lift up.

2. I need the location of the finger. That's why I am using UIView - whole screen is a target.

I tried:

1. Setting up my UIViewController as the delegate for UITapGestureRecognizer. Function gestureRecognizer:shouldReceiveTouch: works but locationOfTouch:inView: always returns top left point as touched location.

2. Casting UIView to UIControl and calling addTarget:action:forControlEvents: works too but doesn't return the location.

3. Subclassing UIGestureRecognizer. I tried this but it din't work because I don't know how to override touchesBegan:WithEvent:

I feel like custom gesture is the only way, can you please refer me to the manual or sample code? Or maybe other solutions? Thank you for reading.

网友答案:

UIPanGestureRecognizer

You can use UIPanGestureRecognizer to capture the event.

Add Gesture to your view.

//Method to call for your gesture.
self.panRecognizer = UIPanGestureRecognizer(target: self, action: "panView:")

//Set delegate of Gesture
self.panRecognizer.delegate = self

//Add Gesture to the UIView
self.view.addGestureRecognizer(self.panRecognizer)

Track User event.

//Capture Gesture
func panView(recognizer: UIPanGestureRecognizer){
    switch recognizer.state {
    case UIGestureRecognizerState.Began: break
    case UIGestureRecognizerState.Changed: break
    case UIGestureRecognizerState.Ended: break
    default:
        break
    }
}

touchesBegan:

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    //Get Touch Point
    let Point = touches.first!.locationInView(self.view)
}
相关阅读:
Top