2011-09-12 2 views
2

私はこの上記の方法では2つのメソッドを呼び出したいiPhoneのプログラミングでtouchMovedメソッドの方向を知るには?

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

-(void) movingUp{ 

    } 

    -(void) movingDown { 

    } 

私は、ユーザーが自分のタッチ上方向に移動する際にmoveUpというメソッドを呼び出したい、とmoveDownの際に、ユーザー、 がどのように下方向に触れます私はこれを行います ジェスチャーを使用しましたが、上下に動いているワンタッチを感知していますが、タッチのユーザーの動きに応じて、これらのメソッドを何度も何度も呼び出す必要があります。

Help!

答えて

2

touchesMoved:はタッチセットで呼び出されます。あなたはそれを使用することができます。

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

/Users/jahangirnawaz/Desktop/AlarmProject/Classes/AlarmProjectViewController.m:619:0 /Users/jahangirnawaz/Desktop/AlarmProject/Classes/AlarmProjectViewController.m:619:警告:互換性がありませんObjective-C types 'struct AlarmProjectViewController *'、expected構造体UIView * 'locationInView:'の別のObjective-C型から引数1を渡すとき ウォーリングとクラッシュ –

+2

ああ、それはviewControllerにメソッドを実装しているからです。ちょうど 'self'を 'self.view'と置き換えてください:) –

+1

あなたの質問を編集してください。ここでは、動かないでしょう.-P –

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

    } 
+0

はUITouchをbegintouchですか? –

+0

あなたのコードで宣言されていないデータ型を指定してください –

+0

私は編集しました – Mahesh

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

    } 
関連する問題