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

发布于 2024-12-04 06:50:29 字数 344 浏览 0 评论 0原文

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

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

 -(void) movingUp{

  }

  -(void) movingDown {

  }

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

帮助!

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

I want to call two methods in this above method,

 -(void) movingUp{

  }

  -(void) movingDown {

  }

I want to call moveUp Method when user will move his touch upward direction, and moveDown when user will touch downward direction,
How can I do this,
I used gestures, but it is sensing one touch moving up or down, but I want call these methods again and again, as per user movement of touch, either upside or downwards

Help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

枉心 2024-12-11 06:50:29

TouchesMoved: 被调用时设置了 Touches ;你可以使用它:

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

The touchesMoved: is called with a touches set ; you can use it :

CGFloat diff = [[touches anyObject] locationInView:self].y - [[touches anyObject] previousLocationInView:self].y;
if (diff > 0) {
    // moving down
} else {
    // moving up
}
作妖 2024-12-11 06:50:29

CGPoint 开始点、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)];
                }
             }
             }
              }
        }

    }

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

    }
善良天后 2024-12-11 06:50:29
   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];
      }
      }

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

  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文