UITouch touchesMoved Finger Direction and Speed

后端 未结 2 1498
慢半拍i
慢半拍i 2020-12-31 07:42

How can I get Speed and Direction of finger movements in touchmoved function?

I want to get the finger speed and finger direction and apply it in a UIView class dire

相关标签:
2条回答
  • 2020-12-31 08:16

    Direction will be determined from the values of "location" and "prevLocation" in touchesMoved. Specifically, location will contain the new point of the touch. For example:

    if (location.x - prevLocation.x > 0) {
        //finger touch went right
    } else {
        //finger touch went left
    }
    if (location.y - prevLocation.y > 0) {
        //finger touch went upwards
    } else {
        //finger touch went downwards
    }
    

    Now touchesMoved will get called many times for a given finger movement. It will be key to your code to compare an initial value when the finger first touches the screen, with a value of the CGPoint when the movement is finally complete.

    0 讨论(0)
  • 2020-12-31 08:27

    why not just the below as a variation on obuseme's response

    -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    
             UITouch *aTouch = [touches anyObject];
             CGPoint newLocation = [aTouch locationInView:self.view];
             CGPoint prevLocation = [aTouch previousLocationInView:self.view];
    
             if (newLocation.x > prevLocation.x) {
                     //finger touch went right
             } else {
                     //finger touch went left
             }
             if (newLocation.y > prevLocation.y) {
                     //finger touch went upwards
             } else {
                     //finger touch went downwards
             }
    }
    
    0 讨论(0)
提交回复
热议问题