如何知道iphone编程中touchMoved方法的方向?

时间:2011-09-12 12:07:50

标签: iphone uikit

    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
 }

我想在上面的方法中调用两个方法,

 -(void) movingUp{

  }

  -(void) movingDown {

  }

我想在用户向上移动触摸方向时调用moveUp方法,在用户向下触摸方向时调用moveDown, 我怎样才能做到这一点, 我使用手势,但它感觉到一个触摸向上或向下移动,但我想一次又一次地调用这些方法,根据用户的触摸移动,向上或向下

帮助!

3 个答案:

答案 0 :(得分:2)

touchesMoved:用触摸设置调用;你可以用它:

CGFloat diff = [[touches anyObject] locationInView:self].y - [[touches anyObject] previousLocationInView:self].y;
if (diff > 0) {
    // moving down
} else {
    // moving up
}

答案 1 :(得分:0)

CGPoint beginPoint,tappoint;

 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        beginPoint = [touch locationInView:self];//or self.view

    }

    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
        NSSet *allTouches = [event allTouches];

        switch ([allTouches count]) {
              case 1:
              {
                  UITouch *touch = [[allTouches allObjects] objectAtIndex:0];

              switch ([touch tapCount])
              {
             case 1: //Single Tap.
             {
                tappoint = [touch locationInView:self];
                if(tappoint.y-beginPoint.y>0)
                {
                    [self performSelector:@selector(movingUp)];
                }   else
                {
                    [self performSelector:@selector(movingDown)];
                }
             }
             }
              }
        }

    }

答案 2 :(得分:0)

   Declare initialPoint and endPoint as Global variables in .h file :
   CGPoint initialPoint,endPoint;

  And in .m file write below code :

  - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  {

      initialPoint = [[touches anyObject]locationInView:self.view]; 
  }

  - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
 {
     endPoint = [[touches anyObject]locationInView:self.view];
     if ((endPoint.y - initialPoint.y) > 100.f) {

          UIAlertView *obj = [[UIAlertView alloc]initWithTitle:@"Touches and Events" 
    message:@"Swipe Down" delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:@"ok",nil];
          [obj show];
          [obj release];
      }
      else if((endPoint.y - initialPoint.y) < -100.f) {
          UIAlertView *obj1 = [[UIAlertView alloc]initWithTitle:@"Touches and Events" 
   message:@"Swipe Up" delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:@"ok",nil];
          [obj1 show];
          [obj1 release];
   endPoint = [[touches anyObject]locationInView:self.view];
      if ((endPoint.x - initialPoint.x) > 100.f) {

          UIAlertView *obj = [[UIAlertView alloc]initWithTitle:@"Touches and Events" 
  message:@"Swipe right" delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:@"ok",nil];
          [obj show];
          [obj release];
      }
      else if((endPoint.x - initialPoint.x) < -100.f) {
          UIAlertView *obj1 = [[UIAlertView alloc]initWithTitle:@"Touches and Events" 
  message:@"Swipe left" delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:@"ok",nil];
          [obj1 show];
          [obj1 release];
      }
      }

  }