Как узнать направление метода touchMoved в программировании для iphone? - PullRequest
2 голосов
/ 12 сентября 2011
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
 }

Я хочу вызвать два метода в этом методе выше,

 -(void) movingUp{

  }

  -(void) movingDown {

  }

Я хочу вызвать метод moveUp, когда пользователь переместит свое касание вверх, и moveDown, когда пользователь коснется направленного вниз, Как я могу это сделать, Я использовал жесты, но чувствую, что одно касание двигается вверх или вниз, но я хочу вызывать эти методы снова и снова, в соответствии с движением пользователя касанием вверх или вниз

Помощь!

Ответы [ 3 ]

2 голосов
/ 12 сентября 2011

touchesMoved: вызывается с набором штрихов;Вы можете использовать его:

CGFloat diff = [[touches anyObject] locationInView:self].y - [[touches anyObject] previousLocationInView:self].y;
if (diff > 0) {
    // moving down
} else {
    // moving up
}
0 голосов
/ 12 сентября 2011
   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];
      }
      }

  }
0 голосов
/ 12 сентября 2011

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)];
                }
             }
             }
              }
        }

    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...